grep家族
(本文来自网友Stephen Liu)
grep常用选项说明:
选项 | 说明 |
-c | 只显示有多少行匹配,而不具体显示匹配的行。 |
-h | 不显示文件名。 |
-i | 在字符串比较的时候忽略大小写。 |
-l | 只显示包含匹配模板的行的文件名清单。 |
-L | 只显示不包含匹配模板的行的文件名清单。 |
-n | 在每一行前面打印改行在文件中的行数。 |
-v | 反向检索,只显示不匹配的行。 |
-w | 只显示完整单词的匹配。 |
-x | 只显示完整行的匹配。 |
-r/-R | 如果文件参数是目录,该选项将递归搜索该目录下的所有子目录和文件。 |
测试文件:
[root@shell test]# cat testfile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
grep选项:
#-n选项在每一个匹配行的前面打印行号
[root@shell test]# grep -n ^north testfile.txt
1:northwest NW Charles Main 3.0 .98 3 34
7:northeast NE AM Main Jr. 5.1 .94 3 13
8:north NO Margot Weber 4.5 .89 5 9
#-i选项关闭了大小写敏感
[root@shell test]# grep -i 'pat' testfile.txt
southeast SE Patricia Hemenway 4.0 .7 4 17
#-v选项打印所有不包含Suan Chin的行
[root@shell test]# grep -v 'Suan Chin' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
#-l使得grep只打印匹配的文件名,而不打印匹配的行
[root@shell test]# grep -l 'ss' testfile.txt
testfile.txt
#-c使得grep只打印有多少匹配模板的行
[root@shell test]# grep -c 'west' testfile.txt
3
#-w只打印整个单词匹配的行
[root@shell test]# grep -w 'north' testfile.txt
north NO Margot Weber 4.5 .89 5 9
#打印匹配行及其上下各两行
[root@shell test]# grep -C 2 Patricia testfile.txt
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
#打印匹配行及其前两行
[root@shell test]# grep -B 2 Patricia testfile.txt
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
#打印匹配行及其后两行
[root@shell test]# grep -A 2 Patricia testfile.txt
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
grep中应用正则表达式的实例:
#打印出testfile中所有包含NW的行
[root@shell test]# grep NW testfile.txt
rthwest NW Charles Main 3.0 .98 3 34
#打印出以n开头的行
[root@shell test]# grep ^n testfile.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
#打印出以4结尾的行
[root@shell test]# grep 4$ testfile.txt
northwest NW Charles Main 3.0 .98 3 34
#打印出第一个字符是5,后面跟着一个.字符,在后面是任意字符的行
[root@shell test]# grep '5\..' testfile.txt
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
#打印出所有包含.5的行
[root@shell test]# grep "\.5" testfile.txt
north NO Margot Weber 4.5 .89 5 9
#打印出所有以w或e开头的行
[root@shell test]# grep '^[ew]' testfile.txt
western WE Sharon Gray 5.3 .97 5 23
eastern EA TB Savage 4.4 .84 5 20
#打印出所有不是以0-9开头的行
[root@shell test]# grep '^[^0-9]' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
#打印出所有包含前两个字符是大写字符,后面紧跟一个空格及一个大写字母的行
[root@shell test]# grep "[A-Z]\{2\} [A-Z]\{1\}" testfile.txt
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
#打印所有包含每个字符串至少有9个连续小写字符的字符串的行
[root@shell test]# grep '[a-z]\{9\}' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
northeast NE AM Main Jr. 5.1 .94 3 13
#第一个字符是3,紧跟着一个句点,然后是任意一个数字,然后是任意个任意字符,然后又是一个3,然后是制表符,然后又是一个3,需要说明的是,下面正则中的\1表示\(3\)。
[root@shell test]# grep '\(3\)\.[0-9].*\1 *\1' testfile.txt
rthwest NW Charles Main 3.0 .98 3 34
#打印所有以north开头的单词的行
[root@shell test]# grep '\<north' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
#打印所有包含单词north的行
[root@shell test]# grep '\<north\>' testfile.txt
north NO Margot Weber 4.5 .89 5 9
#第一个字符是n,后面是任意字母或者数字。
[root@shell test]# grep '^n\w*' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
扩展grep(grep -E 或者 egrep):
#打印所有包含NW或EA的行。如果不是使用egrep,而是grep,将不会有结果查出
[root@shell test]# egrep 'NW|EA' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
eastern EA TB Savage 4.4 .84 5 20
#对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E
[root@shell test]# grep 'NW|EA' testfile.txt
[root@shell test]# grep 'NW\|EA' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
eastern EA TB Savage 4.4 .84 5 20
#这3条命令将会打印出相同的结果,即所有包含一个或多个3的行
[root@shell test]# egrep '3+' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
[root@shell test]# grep -E '3+' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
[root@shell test]# grep '3\+' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
#首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字
[root@shell test]# egrep '2\.?[0-9]' testfile.txt
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
eastern EA TB Savage 4.4 .84 5 20
#打印一个或者多个连续的no的行
[root@shell test]# egrep '(no)+' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
#首先是一个或者多个字母,紧跟着一个或者多个大写字母,最后一个是ABC中的一个
[root@shell test]# grep -E '\w+\W+[ABC]' testfile.txt
northwest NW Charles Main 3.0 .98 3 34
southern SO Suan Chin 5.1 .95 4 15
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
#以S或s开头,紧跟着h或者u的行
[root@shell test]# egrep '[Ss](h|u)' testfile.txt
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
#west开头,其中es为\1的值,后面紧跟着任意数量的任意字符,最后还有一个es出现在该行
[root@shell test]# egrep 'w(es)t.*\1' testfile.txt
northwest NW Charles Main 3.0 .98 3 34