元字符

功能

示例

描述

^

行首定位符

'^love'

匹配以love开头的行

$

行尾定位符

'love$'

匹配以love结尾的行

.

匹配一个字符

'l..e

匹配一个l后面接2个字符,再接一个e

*

匹配0个或多个

'l*ove'

匹配0个或多个l后面跟ove

[]

匹配其中的任意一个

'[Ll]ove'

匹配Love或love

[^]

匹配不在组内的字符

'[^A-K]ove'

匹配不在A-K直接的字符,并且后面跟着ove的行

\<

词首定位符

'\<love'

匹配以love开头的词

\>

词尾定位符

'love\>'

匹配以love结尾的词

\(..\)

匹配到的字符

'\(love\)'

love被标记为1

x\{m\}

字符x重复出现的次数

o\{5\}

o出现5次

x\{m,\}

至少m次

o\{5,\}

o出现至少5次

x\{m,n\}

m到n次

o\{5,10\}

o出现5-10次

 

扩展元字符

+

匹配一个或多个加号前面的字符

?

匹配0个或一个前导字符

a|b

匹配a或b

( )

字符组

代码:

[root@backup test]# cat test.txt 
orthwest        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

 ^  以x开头,所有的内容都有在双引号内

[root@backup test]# grep "^n" test.txt 
northeast         NE    AM Main Jr.         5.1    .94    3    13
north             NO    Margot Weber        4.5    .89    5     9

$ 以x结尾

[root@backup test]# grep "3$" test.txt 
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

^$ 空行,-n显示行号

[root@backup test]# grep -n "^$" test.txt 
3:

-v 排除

[root@backup test]# grep -v "3" test.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
north            NO    Margot Weber         4.5    .89    5     9

 . 代表切只能代表任意一个字符

[root@backup test]# grep "n.r" test.txt 
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@backup test]# grep "\.7" test.txt 
southwest         SW    Lewis Dalsass        2.7    .8    2    18
southeast         SE    Patricia Hemenway    4.0    .7    4    17
central           CT    Ann Stephens         5.7    .94   5    13

* 重复0个或多个前面的字符

[root@backup test]# grep "saa*" test.txt 
southwest        SW    Lewis Dalsass        2.7    .8    2    18

.* 所有字符

[root@backup test]# grep ".*" test.txt 
orthwest           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

[ABC] 匹配字符集内任意一个字符

[root@backup test]# grep "[BAC]" test.txt 
orthwest           NW    Charles Main        3.0    .98    3    34
southern           SO    Suan Chin           5.1    .95    4    15
eastern            EA    TB Savage           4.4    .84    5    20
northeast          NE    AM Main Jr.         5.1    .94    3    13
central            CT    Ann Stephens        5.7    .94    5    13

匹配以W或者e开头的行

[root@backup test]# grep "^[we]" test.txt 
western            WE    Sharon Gray        5.3    .97    5    23
eastern            EA    TB Savage          4.4    .84    5    20

打印两个大写字母,然后接一个空格和大写字母的行

[root@backup test]# grep "[A-Z][A-Z] [A-Z]" test.txt 
eastern            EA    TB Savage          4.4    .84    5    20
northeast          NE    AM Main Jr.        5.1    .94    3    13

[0-9] 匹配所有数字

[a-z] 匹配所有小写字母

[0-9a-Z] 数字和字母

[^ABC] 不匹配字符集内任意一个字符

x\{m\} 字符x出现m次

[root@backup test]# cat test2.txt 
23300d
h000000j
h000j00j
h000012100j
90000000000234
[root@backup test]# grep "0\{6\}" test2.txt 
h000000j
90000000000234

x\{m,\} 字符x出现至少m次

[root@backup test]# grep "0\{3,\}" test2.txt 
h000000j
h000j00j
h000012100j
90000000000234

x\{m,n\} 字符x出现m-n次

[root@backup test]# grep "0\{4,6\}" test2.txt 
h000000j
h000012100j
90000000000234

打印有连续9个小写字母的行

[root@backup test]# grep "[a-z]\{9\}" test.txt 
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

打印以AM单词开头的行

[root@backup test]# grep "\<AM" test.txt 
northeast         NE    AM Main Jr.        5.1    .94    3    13

打印含有north单词的行

[root@backup test]# grep "\<north\>" test.txt 
north            NO    Margot Weber        4.5    .89    5     9

打印包含单词north的行

[root@backup test]# grep -w "north" test.txt 
north            NO    Margot Weber        4.5    .89    5     9

i 忽略大小写字母

[root@backup test]# grep -i "pat" test.txt 
southeast         SE    Patricia Hemenway    4.0    .7    4    17

显示出文件内容包含0的文件名

[root@backup test]# grep -l "0" *
test2.txt
test.txt

打印文件中包含0的行数

[root@backup test]# cat test2.txt 
23300d
h000000j
h000j00j
h000012100j
90000000000234
[root@backup test]# grep -c "0" test2.txt 
5

grep 第一个为要匹配的内容,其余都为文件名

[root@backup test]# grep 0 test.txt test2.txt 
test.txt:orthwest          NW    Charles Main         3.0    .98    3    34
test.txt:southeast         SE    Patricia Hemenway    4.0    .7     4    17
test.txt:eastern           EA    TB Savage            4.4    .84    5    20
test2.txt:23300d
test2.txt:h000000j
test2.txt:h000j00j
test2.txt:h000012100j
test2.txt:90000000000234

-h选项可以不打印头部信息

[root@backup test]# grep "0" test.txt  test2.txt 
test.txt:orthwest          NW    Charles Main         3.0    .98    3    34
test.txt:southeast         SE    Patricia Hemenway    4.0    .7     4    17
test.txt:eastern           EA    TB Savage            4.4    .84    5    20
test2.txt:23300d
test2.txt:h000000j
test2.txt:h000j00j
test2.txt:h000012100j
test2.txt:90000000000234
[root@backup test]# grep -h "0" test.txt  test2.txt 
orthwest          NW    Charles Main         3.0    .98    3    34
southeast         SE    Patricia Hemenway    4.0    .7     4    17
eastern           EA    TB Savage            4.4    .84    5    20
23300d
h000000j
h000j00j
h000012100j
90000000000234

打印包含NW或EA的行 egrep=grep -E

[root@backup test]# grep -E "NW|EA" test.txt 
orthwest           NW    Charles Main        3.0    .98    3    34
eastern            EA    TB Savage           4.4    .84    5    20

打印一个或多个数字2

[root@backup test]# grep  -E "2+" test.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的行,可以是no,nono,nonono等

[root@backup test]# grep -E "(no)+" test.txt 
northeast         NE    AM Main Jr.         5.1    .94    3    13
north             NO    Margot Weber        4.5    .89    5     9

匹配Suan Chin及其前后各一行-C1=-1

[root@backup test]# grep -C1 "Suan Chin" test.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

匹配Suan Chin及其后一行

[root@backup test]# grep -A1 "Suan Chin" test.txt 
southern          SO    Suan Chin        5.1    .95    4    15
southeast         SE    Patricia Hemenway    4.0    .7    4    17

匹配Suan Chin及其前一行

[root@backup test]# grep -B1 "Suan Chin" test.txt 
southwest        SW    Lewis Dalsass        2.7    .8     2    18
southern         SO    Suan Chin            5.1    .95    4    15

没有好的案例

[root@backup test]# cat test3.txt 
aa bb cc dd aaaabb
[root@backup test]# grep "\(aa\)\1" test3.txt 
aa bb cc dd aaaabb

-s选项表示不显示不存在或无匹配文本的错误信息,默认情况下,grep在待搜索文件不存在或搜索不到符合模式的文本行时将打印错误信息

[root@backup test]# grep "NW" test.txt test12.txt 
test.txt:orthwest        NW    Charles Main        3.0    .98    3    34
grep: test12.txt: No such file or directory
[root@backup test]# grep -s  "NW" test.txt test12.txt 
test.txt:orthwest        NW    Charles Main        3.0    .98    3    34

其他参数

选项

说明

-r

递归查询

-c

只打印匹配的行数,不显示匹配的内容。

-i

忽略大小写

-l

打印匹配模板的文件清单

-n

在匹配的行前面打印行号

-s

只显示报错信息

-v

显示不匹配的行

-w

作为单词来查找

-h

不显示文件名

-q

取消显示,只返回退出状态。0则表示找到了匹配的行