grep命令2

回复 收藏

[root@awulinux ~]# grep -n 'awu' 2.txt     过滤出带有某个关键字的行并输出行号

[root@awulinux ~]# grep -n -v 'awu' 2.txt      过滤出不带有某个关键字的行并输出行号

[root@awulinux ~]# gerp '[0-9]' 2.txt     过滤出所有包含数字的行

[root@awulinux ~]# gerp -v '[0-9]' 2.txt    过滤出所有不包含数字的行   

[root@awulinux ~]# grep '[aN]' 2.txt     过滤出含有a或者N的行

[root@awulinux ~]# grep '[a-zA-Z]' 2.txt    过滤出所有包含字母的行

[root@awulinux ~]# grep -v '[a-zA-Z]' 2.txt     过滤出所有不包含字母的行

[root@awulinux ~]# grep '^[a-zA-Z]' 2.txt      过滤出以英文字母开头的行

[root@awulinux ~]# grep '^[0-9]' 2.txt        过滤出以数字开头的行

[root@awulinux ~]# grep -v '^[0-9]' 2.txt     过滤出不以数字开头的行

[root@awulinux ~]# grep '[^0-9]' 2.txt      过滤出不包含数字的行

[root@awulinux ~]# grep  '^[^0-9]' 2.txt     过滤出不以数字开头的行,但是包括特殊符号开头,以及空行

[root@awulinux ~]# grep '^$' 2.txt     过滤出空行

[root@awulinux ~]# grep -v '^$' 2.txt    过滤出空行之外的行

[root@awulinux ~]# grep 'r.o' 2.txt   .表示任意的一个字符  

[root@awulinux ~]# grep 'r*o' 2.txt    *表示零个或多个前面的字符,然后*后面的字符  也就是零个或多个r,以及o

[root@awulinux ~]# grep 'r.*o' 2.txt     .*表示零个或多个任意字符,空行也包括在内   r开头o结尾

[root@awulinux ~]# grep 'r\?o' 2.txt    ?表示零个或一个?前面的字符  即匹配o或者ro


[root@awulinux ~]# grep -E  'r?o' 2.txt    这里的-E相当于上面的\

[root@awulinux ~]# grep -E == egrep     可以用grep -E代替egrep
egrep工具是grep工具的扩展,它可以实现所有grep的功能

[root@awulinux ~]# grep --color 'r\?o' 2.txt == egrep --color 'r?o' 2.txt^C
[root@awulinux ~]# egrep --color 'r?o' 2.txt      匹配零个或者1个问号前面的字符,比如roo,ro,oo,o

[root@awulinux ~]# egrep --color 'r+o' 2.txt      匹配1个或者1个以上+前面的字符

[root@awulinux ~]# grep -E 'r+o' 2.txt
[root@awulinux ~]# grep 'r\+o' 2.txt      使用grep需要使用脱义\

.  表示任意一个字符(包括特殊字符、空格、下划线等)
*表示零个或者多个*前面的字符
.*表示任意个任意字符,包括空行
+表示1个或者多个+前面的字符
?表示零个或者1个?前面的字符
其中,+和? grep不支持,要想支持需加-E,egrep则都支持

[root@awulinux ~]# egrep --color 'root|nologin' 2.txt    匹配root或者nologin,这里'|'表示或者

[root@awulinux ~]# grep --color 'root' 2.txt |grep --color 'nologin'  这里用两个"grep"以及"|"表示并且

[root@awulinux ~]# egrep --color '(rr)+' 2.txt     用括号表示一个整体,表示匹配1个或者1个以上rr
[root@awulinux ~]# grep -E --color '(rr)+' 2.txt

[root@awulinux ~]# grep -E --color '(rr){1,3}' 2.txt     表示匹配1到3个rr
[root@awulinux ~]# egrep --color '(rr){1,3}' 2.txt

[root@awulinux ~]# egrep --color '(rr){5}' 2.txt    表示匹配5对rr,即rrrrrrrrrr

[root@awulinux ~]# grep --color '\(rr\)\{5,6\}' 2.txt   这里使用grep需要用到"\"
[root@awulinux ~]# grep -E --color '(rr){5,6}' 2.txt

? + () {} | 使用grep需要用"\"。


















2016-06-11 23:28 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: