grep---Globel Search Regular Expression and Printing out the line全面搜索正则表达式并把行打印出来
功 能:查找文件里符合条件的字符串
说 明:grep指令是一种强大的文本搜索工具,是一个对行进行搜索的操作,它能使用正则表达式搜索文本,用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设grep指令会把含有范本样式的那一列显示出来。若不指定任何文件名称,或是所给予的文件名为“-”,则grep指令会从标准输入设备读取数据。
Unix的grep家族包括grep、egrep和fgrep。 egrep表示扩展的grep,相比grep支持更多的元字符(元字符就是指那些在正则表达式中具有特殊意义的专用字符),"grep -E"相当于egrep。fgrep是fast grep,不支持元字符,但是搜索速度更快。grep搜索的结果被送到屏幕,不影响原文件内容。
grep搜索建议:
字符串建议使用双引号:grep "ZOO_INFO" zookeeper.log
正则匹配应使用单引号:grep ‘\<ZOO_INFO\>’ zookeeper.log
参 数:
1 2 3 | [root@localhost grub] # grep "title" grub.conf title Red Hat Enterprise Linux Server (2.6.18-308.el5) [root@localhost grub] # |
-A<显示列数>或--after-context=<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之后的内容。
1 2 3 4 5 6 7 8 | [root@localhost ~] # cat /boot/grub/grub.conf |grep -A 3 title title Red Hat Enterprise Linux Server (2.6.18-308.el5) # password --md5 $1$GnvKI1$knaWZAwoGoee/4sjZ4t4c1
root (hd0,0)
kernel /vmlinuz-2 .6.18-308.el5 ro root=LABEL= /1 rhgb quiet [root@localhost ~] # cat /boot/grub/grub.conf |grep title title Red Hat Enterprise Linux Server (2.6.18-308.el5) [root@localhost ~] # |
-B<显示列数>或--before-context=<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前的内容。
1 2 3 4 5 | [root@localhost ~] # cat /boot/grub/grub.conf |grep -B 2 title splashp_w_picpath=(hd0,0) /grub/splash .xpm.gz hiddenmenu title Red Hat Enterprise Linux Server (2.6.18-308.el5) [root@localhost ~] # |
-C<显示列数>或--context=<显示列数>或-<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前后的内容。
1 2 3 4 5 6 7 | [root@localhost ~] # cat /boot/grub/grub.conf |grep -C 2 title splashp_w_picpath=(hd0,0) /grub/splash .xpm.gz hiddenmenu title Red Hat Enterprise Linux Server (2.6.18-308.el5) # password --md5 $1$GnvKI1$knaWZAwoGoee/4sjZ4t4c1
root (hd0,0) [root@localhost ~] # |
-c或--count 计算符合范本样式的列数。
1 2 3 4 5 6 7 | [root@localhost ~] # cat /boot/grub/grub.conf |grep -c 2.6.18-308.el5 3 [root@localhost ~] # cat /boot/grub/grub.conf |grep 2.6.18-308.el5 title Red Hat Enterprise Linux Server (2.6.18-308.el5)
kernel /vmlinuz-2 .6.18-308.el5 ro root=LABEL= /1 rhgb quiet
initrd /initrd-2 .6.18-308.el5.img [root@localhost ~] # |
-i或--ignore-case 忽略字符大小写的差别。
-r或--recursive 递归的搜索,当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
-v或--revert-match 反转查找。
-s或--no-messages 不显示错误信息
-n或--line-number 在显示符合范本样式的那一列之前,标示出该列的列数编号。
1 2 3 | [root@localhost ~] # cat /boot/grub/grub.conf |grep -n title 14:title Red Hat Enterprise Linux Server (2.6.18-308.el5) [root@localhost ~] # |
-o:只显示被模式匹配到的字符串,而不是整个行
1 2 3 | [root@localhost grub] # cat /boot/grub/grub.conf |grep -o title title [root@localhost grub] # |
grep支持基本正则表达式的元字符:
符号“^”表示以什么字符开头,“^word”表示以“word”开头,
1 2 3 4 5 | [root@localhost grub] # grep "^timeout" /boot/grub/grub.conf timeout=5 [root@localhost grub] # cat /boot/grub/grub.conf |grep "^timeout" timeout=5 [root@localhost grub] # |
符号“$”表示以什么字符结尾,“word$”表示以“word”结尾,
1 2 3 4 5 | [root@localhost grub] # grep "gz$" /boot/grub/grub.conf splashp_w_picpath=(hd0,0) /grub/splash .xpm.gz [root@localhost grub] # cat /boot/grub/grub.conf |grep "gz$" splashp_w_picpath=(hd0,0) /grub/splash .xpm.gz [root@localhost grub] # |
“^$”则表示空行。
符号”.“表示任意单个字符
1 2 3 4 | #h后面匹配任意单个字符后面紧跟d的行 [root@localhost grub] # cat /boot/grub/grub.conf |grep "h.d" hiddenmenu [root@localhost grub] # |
符号”[]“表示匹配一个指定范围内的字符,'[Gg]rep'匹配Grep和grep
符号"[^]"表示匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-F和H-Z的一个字母开头,紧跟rep的行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ^ 锚定行的开始 如: '^grep' 匹配所有以 grep 开头的行。 $ 锚定行的结束 如: 'grep$' 匹配所有以 grep 结尾的行。 . 匹配一个非换行符的字符 如: 'gr.p' 匹配例如 grep 。 * 匹配零个或多个先前字符 如: '*grep' 匹配所有一个或多个空格后紧跟 grep 的行。 .*一起用代表任意字符。 [] 匹配一个指定范围内的字符,如 '[Gg]rep' 匹配Grep和 grep 。 [^] 匹配一个不在指定范围内的字符,如: '[^A-FH-Z]rep' 匹配不包含A-F和H-Z的一个字母开头,紧跟rep的行。 \(..\) 标记匹配字符,如 '\(love\)' ,love被标记为1。 \< 锚定单词的开始,如: '\<grep' 匹配包含以 grep 开头的单词的行。 \> 锚定单词的结束,如 'grep\>' 匹配包含以 grep 结尾的单词的行。 x\{m\} 重复字符x,m次,如: 'o\{5\}' 匹配包含5个o的行。 x\{m,\} 重复字符x,至少m次,如: 'o\{5,\}' 匹配至少有5个o的行。 x\{m,n\} 重复字符x,至少m次,不多于n次,如: 'o\{5,10\}' 匹配5--10个o的行。 \w 匹配文字和数字字符,也就是[A-Za-z0-9],如: 'G\w*p' 匹配以G后跟零个或多个文字或数字字符,然后是p。 \W \w的反置形式,匹配一个或多个非单词字符,如点号句号等。 \b 单词锁定符,如: '\bgrep\b' 只匹配 grep 。 |
本文出自 “水滴石穿” 博客,请务必保留此出处http://pengyl.blog.51cto.com/5591604/1265204