Linux常用基本命令:grep
作者: / 2019-08-02 / 浏览次数:

作用:从文本文件或管道数据流中筛选匹配的行及数据,配合正则表达式一起使用,功能更加强大。

格式:

grep [options] [pattern] [file]

 

1,匹配包含"k8凯发官方网址ghostwu"的行

ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt 
 1 my name is ghostwu
 2 how are you
 3 fine think you
 4 My name is Ghostwu
 5 what's your name?
 6 my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu
my name is ghostwu2

 

2,-v: 不包含,相当于取反

ghostwu@dev:~/linux/grep$ grep -v "ghostwu" ghostwu.txt 
how are you
fine think you
My name is Ghostwu
what's your name?
ghostwu@dev:~/linux/grep$ 

 

3,-n 添加行号

ghostwu@dev:~/linux/grep$ grep -n "ghostwu" ghostwu.txt 
1:my name is ghostwu
6:my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -vn "ghostwu" ghostwu.txt 
2:how are you
3:fine think you
4:My name is Ghostwu
5:what's your name?
7:

 

4,-E,使用扩展的egrep命令,模式中可以用正则表达式

ghostwu@dev:~/linux/grep$ cat ghostwu.txt 
my name is ghostwu
how are you
fine think you
My name is Ghostwu
what's your name?
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -E "my|your" ghostwu.txt 
my name is ghostwu
what's your name?
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -Ev "my|your" ghostwu.txt 
how are you
fine think you
My name is Ghostwu
ghostwu@dev:~/linux/grep$ grep -En "my|your" ghostwu.txt 
1:my name is ghostwu
5:what's your name?
6:my name is ghostwu2

 

5,-i选项,不区分大小写

ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -i "ghostwu" ghostwu.txt 
my name is ghostwu
My name is Ghostwu
my name is ghostwu2

 

6,-c :统计匹配的行数,不是匹配字符串的次数

ghostwu@dev:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 
ghostwu@dev:~/linux/grep$ grep -ci "ghostwu" ghostwu.txt 
3
【某某业务】网站建设、网站设计、服务器空间租售、网站维护、网站托管、网站优化、百度推广、自媒体营销、微信公众号
如有意向---联系我们
热门栏目
热门资讯

网站建设 网站托管 成功案例 新闻动态 关于我们 联系我们 服务器空间 加盟合作 网站优化

备案号: 

公司地址:江苏省南京市玄武区玄武湖 咨询QQ:9490489 手机: 电话: