Contents
3.7. 7 三剑客之grep¶
3.7.1. 7.1 概念¶
grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。
3.7.2. 7.2 语法格式¶
grep [OPTION]… PATTERN [FILE]
选项¶
grep --help
选项 说 明
-c 只打印匹配的文本行的行数,不显示匹配的内容
-i 匹配时忽略字母的大小写
-h 当搜索多个文件时,不显示匹配文件名前缀
-l 只列出含有匹配的文本行的文件的文件名,而不显示具体的匹配的内容
-n 列出所有的匹配的文本行,并显示行号
-s 不显示关于不存在或者无法读取文件的错误信息
-v 只显示不匹配的文本行
-w 匹配整个单词
-x 匹配整个文本行
-r 递归搜索,不仅搜索当前目录,还要搜索其各级子目录
-q 禁止输出任何匹配结果,而是以退出状态码的形式表示搜索是否成功,其中0表示找到了匹配的文本行
-b 打印匹配的文本行到文件头的偏移量,以字节为单位
-E 支持扩展正则表达式
-P 支持Perl正则表达式
-F 不支持正则表达式,将模式按字面意义匹配
模式¶
基本正则表达式元字符:
. :匹配任意单个字符
[] :匹配指定范围内的字符
[^]:匹配指定范围外的任意字符
次数匹配(贪婪模式)
*:匹配其前的字符0,1或者多次
?:匹配其前的字符0或1次
\{m,n\}:
\(m,\):至少m次
\{0,n\}:至多n次
\{m\}:m次
锚定符:
r..t :root chroot
* 单词锚定:
\<:锚定词首:\<r..t, \b
\>:锚定词尾:root\>
* 行首行末锚定:
^: ^root, 行首
$: root$ 行尾
.*: 任意长度的任意字符
分组: () (abc)
引用:
\1 :后向引用,引用前面的第一个左括号与与之对应的右括号中的模式所匹配到的内容
3.7.3. 7.3 使用举例¶
dmesg |grep -n eth0
grep -E '/.{2,3}' /etc/passwd
$ grep ‘test’ d*
显示所有以d开头的文件中包含 test的行。
$ grep ‘test’ aa bb cc
显示在aa,bb,cc文件中匹配test的行。
$ grep ‘[a-z]\{5\}’ aa
显示所有包含每个字符串至少有5个连续小写字符的字符串的行。
$ grep ‘w\(es\)t.*\1′ aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),
这些字符后面紧跟着 另外一个es(\1),找到就显示该行。
如果用egrep或grep -E,就不用”\”号进行转义,直接写成’w(es)t.*\1′就可以了。
grep -i pattern files :不区分大小写地搜索。默认情况区分大小写,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -w pattern files :只匹配整个单词,而不是字符串的一部分(如匹配’magic’,而不是’magical’),
grep -C number pattern files :匹配的上下文分别显示[number]行,
grep pattern1 | pattern2 files :显示匹配 pattern1 或 pattern2 的行,
例如:grep "abc\|xyz" testfile 表示过滤包含abc或xyz的行
grep pattern1 files | grep pattern2 :显示既匹配 pattern1 又匹配 pattern2 的行。
grep -n pattern files 即可显示行号信息
grep -c pattern files 即可查找总行数
显示/proc/meminfo文件中以大小写s开头的行;
# grep "^[sS]" /proc/meminfo
# grep -i "^s" /proc/meminfo
取出默认shell为非bash的用户;
# grep -v "bash$" /etc/passwd | cut -d: -f1
取掉空行
grep -E -v "^$|^#" /etc/httpd/conf/httpd.conf
查看mysql中的库
$(mysql -uroot -p'passwd' -e "show databases;"|egrep -v 'Database|^test|mysql|performance_schema|information_schema')
# grep
## 匹配到1的行
[root@localhost ~]# grep "1" awk.txt
hujianli.wang male 30 021-1111111
jianli.hu Female 25 021-222222
jack.chen Male 35 021-333333
lily.gong Female 20 021-4444444 shanghai
## -i 不区分大小写的匹配
[root@localhost ~]# grep -i "f" awk.txt
jianli.hu Female 25 021-222222
lily.gong Female 20 021-4444444 shanghai
## 匹配以hu开头的行
[root@localhost ~]# grep "^hu" awk.txt
hujianli.wang male 30 021-1111111
#搜索以Good 结尾的行
grep 'Good$' RegExp.txt
#排除空行
[root@localhost ~]# grep -v "^$" sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4 ,this is Third line
this is line 5 this is fifth line
#匹配空行的数量
[root@localhost ~]# grep -c "^$" sed.txt
1
## 搜索字符..匹配
[root@localhost ~]# grep 'g..d' test_grep.txt
good morning teacher
gold sunshine looks beautiful
golden time flies you fell
glad wrong word
gl0d wrong word
gl2d wrong word gl3d
## ^取反匹配
[root@localhost ~]# grep '[^Gg]ood' test_grep.txt
what a delicious food
wrong word gooood
[root@localhost ~]# grep '[Gg]..d' test_grep.txt
good morning teacher
gold sunshine looks beautiful
golden time flies you fell
glad wrong word
gl0d wrong word
gl2d wrong word gl3d
[root@localhost ~]# grep '[Gg][lo].d' test_grep.txt
good morning teacher
gold sunshine looks beautiful
golden time flies you fell
glad wrong word
gl0d wrong word
gl2d wrong word gl3d
## *来匹配
[root@localhost ~]# grep go*d test_grep.txt
good morning teacher
wrong word gooood
## -
[root@localhost ~]# grep 'gl[0-9]d' test_grep.txt
gl0d wrong word
gl2d wrong word gl3d
## 可以使用egrep, egrep支持正则匹配
{
[:alnum:] 文字数字字符
[:alpha:] 文字字符
[:digit:] 数字字符
[:graph:] 非空字符( 非空格、控制字符)
[:lower:] 小写字符
[:cntrl:] 控制字符
[:print:] 非空字符( 包括空格)
[:punct:] 标点符号
[:space:] 所有空白字符( 新行,空格,制表符)
[:upper:] 大写字符
[:xdigit:] 十六进制数字(0-9 ,a-f ,A-F)
搜索以大写字母开头的行
}