参考文献:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html
全称:global search regular expression(RE) and print out the line,即全面搜索正则表达式并把行打印出来
功能:它能使用正则表达式搜索文本,并把匹配的行打印出来,即查找文件里符合条件的字符串。
grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到屏幕,不影响原文件内容。
1. 常用语法:grep [-acinv] [--color=auto] '搜寻字符串' filename
选项与参数:-a :将二进制文件以 text 文件的方式去搜寻
-c :计算找到 '搜寻字符串' 的次数
-i :忽略字符大小写的差别
-n :显示匹配行及行号
-v :反向选择,显示不包含匹配文本的所有行
2. 简单示例:
2.1 将/etc/passwd中出现 root 的行取出来
[root@localhost home]# grep root /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin或[root@localhost home]# cat /etc/passwd | grep rootroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
2.2 将/etc/passwd,有出现 root 的行取出来自动加颜色,并显示这些行在/etc/passwd的行号
如果需求每次使用 grep都要自动上色,就要加上参数--color=auto,这样显得很麻烦,此时可以在 ~/.bashrc 内加上这行:alias grep='grep --color',即设置别名。再输入命令 source ~/.bashrc 来立即生效即可
2.3 将/etc/passwd,出现 root 和没有出现nologin的行取出来
[root@localhost home]# grep root /etc/passwd | grep -v nologinroot:x:0:0:root:/root:/bin/bash
2.4 用 dmesg 列出核心信息,再以 grep 找出eth 那行,在关键字所在行的前两行与后三行也一起显示
[root@localhost home]# dmesg | grep -n -A3 -B2 --color=auto 'eth' #参数信息请查看man手册1538-e1000 0000:02:01.0: PCI INT A -> GSI 19 (level, low) -> IRQ 191539-e1000 0000:02:01.0: setting latency timer to 641540:e1000 0000:02:01.0: eth0: (PCI:66MHz:32-bit) 00:0c:29:82:ba:0a1541:e1000 0000:02:01.0: eth0: Intel(R) PRO/1000 Network Connection1542-ENS1371 0000:02:02.0: PCI INT A -> GSI 16 (level, low) -> IRQ 161543-parport_pc 00:09: reported by Plug and Play ACPI1544-parport0: PC-style at 0x378, irq 7 [PCSPP,TRISTATE]--1552-ip6_tables: (C) 2000-2006 Netfilter Core Team1553-nf_conntrack version 0.5.0 (7906 buckets, 31624 max)1554:e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None1555-type=1305 audit(1477478583.883:3): audit_pid=1128 old=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:auditd_t:s0 res=11556:eth0: no IPv6 routers present
dmesg命令被用于检查和控制内核的环形缓冲区。kernel会将开机信息存储在ring buffer中。您若是开机时来不及查看信息,可利用dmesg来查看。开机信息保存在/var/log/dmesg文件里。
2.5 根据文件内容递归查找目录:
# grep ‘ene’ * 在当前目录搜索带'ene'行的文件
# grep -r ‘ene’ * 在当前目录及其子目录下搜索'ene'行的文件
# grep -l -r ‘ene’ * 在当前目录及其子目录下搜索'ene'行的文件,但是不显示匹配的行,只显示匹配的文件
3. 与正则表达式的运用:
eg: grep -E "[1-9]+" 或 egrep "[1-9]+" >>>egrep是grep的扩展,支持更多的re元字符。如果grep运行不了的正则表达式就换成egrep。
3.1 如果想要搜寻 test 或 taste 这两个字符时,可以发现,其实他们有共同的 't?st' 存在,这个时候可以这样来搜寻
[root@localhost ~]# grep -n 't[ae]st' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good.[]里面不论有几个字节,他都仅代表某一个字节
3.2 搜索有 oo 的行,但不想要 oo 前面有 g,如下:
[root@localhost ~]# grep -n '[^g]oo' regular_express.txt 2:apple is my favorite food. food中的foo符合条件,被显示出来,下同3:Football game is not use feet only. 18:google is the best tools for search keyword. google不符合条件,但是其后还有tools这个单词,故该 行还是被显示出来19:goooooogle yes! goooooogle里面的oo前面可能是o,例如:go(ooo)oogle,故该行也符合需求
3.3 搜索显示oo前面是大写字母的行:
[root@localhost ~]# grep -n '[^a-z]oo' regular_express.txt3:Football game is not use feet only.
3.4 搜索显示结尾行为小数点“.”的行:
[root@localhost ~]# grep -n '\.$' regular_express.txt \表示转义符,详情参考正则表达式1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesn't fit me.
3.5 找出空白行:
[root@localhost ~]# grep -n '^$' regular_express.txt22:正则表达式中表示只有行首跟行尾(^$),即可找出空白行
正则表达式的更多应用请参考其他文档。
4. 补充
4.1 在多个文件中查找: grep "match_pattern" file_1 file_2 file_3 ...
4.2
#只在目录中所有的.php和.html文件中递归搜索字符"main()" grep "main()" . -r --include *.{php,html} #在搜索结果中排除所有README文件 grep "main()" . -r --exclude "README" #在搜索结果中排除filelist文件列表里的文件 grep "main()" . -r --exclude-from filelist
4.3 grep -q "test" filename >>>不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。