grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。html
Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不一样。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把全部的字母都看做单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,再也不特殊。linux使用GNU版本的grep。它功能更强,能够经过-G、-E、-F命令行选项来使用egrep和fgrep的功能。linux
[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename 选项与参数: -a :将 binary 文件以 text 文件的方式搜寻数据 -c :计算找到 '搜寻字符串' 的次数 -i :忽略大小写的不一样,因此大小写视为相同 -n :顺便输出行号 -v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行! --color=auto :能够将找到的关键词部分加上颜色的显示喔!
将/etc/passwd,有出现 root 的行取出来正则表达式
# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 或 # cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号shell
# grep -n root /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 30:operator:x:11:0:operator:/root:/sbin/nologin
在关键字的显示方面,grep 可使用 --color=auto 来将关键字部分使用颜色显示。 这但是个很不错的功能啊!可是若是每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你能够在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来当即生效便可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦express
将/etc/passwd,将没有出现 root 的行取出来sass
# grep -v root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
将/etc/passwd,将没有出现 root 和nologin的行取出来bash
# grep -v root /etc/passwd | grep -v nologin root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,要将捉到的关键字显色,且加上行号来表示:app
[root@www ~]# dmesg | grep -n --color=auto 'eth' 247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 248:eth0: Identified 8139 chip type 'RTL-8139C' 294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1 305:eth0: no IPv6 routers present # 你会发现除了 eth 会有特殊颜色来表示以外,最前面还有行号喔!
在关键字的显示方面,grep 可使用 --color=auto 来将关键字部分使用颜色显示。 这但是个很不错的功能啊!可是若是每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你能够在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来当即生效便可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦工具
用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,在关键字所在行的前两行与后三行也一块儿捉出来显示ui
[root@www ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth' 245-PCI: setting IRQ 10 as level-triggered 246-ACPI: PCI Interrupt 0000:00:0e.0[A] -> Link [LNKB] ... 247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 248:eth0: Identified 8139 chip type 'RTL-8139C' 249-input: PC Speaker as /class/input/input2 250-ACPI: PCI Interrupt 0000:00:01.4[B] -> Link [LNKB] ... 251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66) # 如上所示,你会发现关键字 247 所在的前两行及 248 后三行也都被显示出来! # 这样可让你将关键字先后数据捉出来进行分析啦!
根据文件内容递归查找目录
# grep ‘energywise’ * #在当前目录搜索带'energywise'行的文件 # grep -r ‘energywise’ * #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r ‘energywise’ * #在当前目录及其子目录下搜索'energywise'行的文件,可是不显示匹配的行,只显示匹配的文件
这几个命令很使用,是查找文件的利器。
字符类
字符类的搜索:若是我想要搜寻 test 或 taste 这两个单字时,能够发现到,其实她们有共通的 't?st' 存在~这个时候,我能够这样来搜寻:
[root@www ~]# grep -n 't[ae]st' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good.
其实 [] 里面不论有几个字节,他都谨表明某『一个』字节, 因此,上面的例子说明了,我须要的字串是『tast』或『test』两个字串而已!
字符类的反向选择 [^] :若是想要搜索到有 oo 的行,但不想要 oo 前面有 g,以下
[root@www ~]# grep -n '[^g]oo' regular_express.txt 2:apple is my favorite food. 3:Football game is not use feet only. 18:google is the best tools for search keyword. 19:goooooogle yes!
第 2,3 行没有疑问,由于 foo 与 Foo 都可被接受!
可是第 18 行明明有 google 的 goo 啊~别忘记了,由于该行后面出现了 tool 的 too 啊!因此该行也被列出来~ 也就是说, 18 行里面虽然出现了咱们所不要的项目 (goo) 可是由於有须要的项目 (too) , 所以,是符合字串搜寻的喔!
至於第 19 行,一样的,由于 goooooogle 里面的 oo 前面多是 o ,例如: go(ooo)oogle ,因此,这一行也是符合需求的!
字符类的连续:再来,假设我 oo 前面不想要有小写字节,因此,我能够这样写 [^abcd....z]oo , 可是这样彷佛不怎么方便,由於小写字节的 ASCII 上编码的顺序是连续的, 所以,咱们能够将之简化为底下这样:
[root@www ~]# grep -n '[^a-z]oo' regular_express.txt 3:Football game is not use feet only.
也就是说,当咱们在一组集合字节中,若是该字节组是连续的,例如大写英文/小写英文/数字等等, 就可使用[a-z],[A-Z],[0-9]等方式来书写,那么若是咱们的要求字串是数字与英文呢? 呵呵!就将他所有写在一块儿,变成:[a-zA-Z0-9]。
咱们要取得有数字的那一行,就这样:
[root@www ~]# grep -n '[0-9]' regular_express.txt 5:However, this dress is about $ 3183 dollars. 15:You are the best is mean you are the no. 1.
行首与行尾字节 ^ $
行首字符:若是我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!咱们能够这样作:
[root@www ~]# grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start.
此时,就只剩下第 12 行,由于只有第 12 行的行首是 the 开头啊~此外, 若是我想要开头是小写字节的那一行就列出呢?能够这样:
[root@www ~]# grep -n '^[a-z]' regular_express.txt 2:apple is my favorite food. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 12:the symbol '*' is represented as start. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go.
若是我不想要开头是英文字母,则能够是这样:
[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 21:# I am VBird
^ 符号,在字符类符号(括号[])以内与以外是不一样的! 在 [] 内表明『反向选择』,在 [] 以外则表明定位在行首的意义!
那若是我想要找出来,行尾结束为小数点 (.) 的那一行:
[root@www ~]# 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. 10:motorcycle is cheap than car. 11:This window is clear. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world <Happy> is the same with "glad". 17:I like dog. 18:google is the best tools for search keyword. 20:go! go! Let's go.
特别注意到,由于小数点具备其余意义(底下会介绍),因此必需要使用转义字符(\)来加以解除其特殊意义!
找出空白行:
[root@www ~]# grep -n '^$' regular_express.txt 22:
由于只有行首跟行尾 (^$),因此,这样就能够找出空白行啦!
任意一个字节 . 与重复字节 *
这两个符号在正则表达式的意义以下:
. (小数点):表明『必定有一个任意字节』的意思; * (星号):表明『重复前一个字符, 0 到无穷屡次』的意思,为组合形态
假设我须要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我能够这样作:
[root@www ~]# grep -n 'g..d' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 9:Oh! The soup taste good. 16:The world <Happy> is the same with "glad".
由于强调 g 与 d 之间必定要存在两个字节,所以,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!
若是我想要列出有 oo, ooo, oooo 等等的数据, 也就是说,至少要有两个(含) o 以上,该如何是好?
由于 * 表明的是『重复 0 个或多个前面的 RE 字符』的意义, 所以,『o*』表明的是:『拥有空字节或一个 o 以上的字节』,所以,『 grep -n 'o*' regular_express.txt 』将会把全部的数据都列印出来终端上!
当咱们须要『至少两个 o 以上的字串』时,就须要 ooo* ,亦便是:
[root@www ~]# grep -n 'ooo*' 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. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes!
若是我想要字串开头与结尾都是 g,可是两个 g 之间仅能存在至少一个 o ,亦便是 gog, goog, gooog.... 等等,那该如何?
[root@www ~]# grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
若是我想要找出 g 开头与 g 结尾的行,当中的字符无关紧要
[root@www ~]# grep -n 'g.*g' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 14:The gd software is a library for drafting programs. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go.
由于是表明 g 开头与 g 结尾,中间任意字节都可接受,因此,第 1, 14, 20 行是可接受的喔! 这个 .* 的 RE 表示任意字符是很常见的.
若是我想要找出『任意数字』的行?由于仅有数字,因此就成为:
[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt 5:However, this dress is about $ 3183 dollars. 15:You are the best is mean you are the no. 1.
限定连续 RE 字符范围 {}
咱们能够利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那若是我想要限制一个范围区间内的重复字节数呢?
举例来讲,我想要找出两个到五个 o 的连续字串,该如何做?这时候就得要使用到限定范围的字符 {} 了。 但由于 { 与 } 的符号在 shell 是有特殊意义的,所以, 咱们必需要使用字符 \ 来让他失去特殊意义才行。 至於 {} 的语法是这样的,假设我要找到两个 o 的字串,能够是:
[root@www ~]# grep -n 'o\{2\}' 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. 9:Oh! The soup taste good. 18:google is the best tools for search ke 19:goooooogle yes!
假设咱们要找出 g 后面接 2 到 5 个 o ,而后再接一个 g 的字串,他会是这样:
[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.
若是我想要的是 2 个 o 以上的 goooo....g 呢?除了能够是 gooo*g ,也能够是:
[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
扩展grep(grep -E 或者 egrep):
使用扩展grep的主要好处是增长了额外的正则表达式元字符集。
打印全部包含NW或EA的行。若是不是使用egrep,而是grep,将不会有结果查出。
# egrep 'NW|EA' testfile northwest NW Charles Main 3.0 .98 3 34 eastern EA TB Savage 4.4 .84 5 20
对于标准grep,若是在扩展元字符前面加\,grep会自动启用扩展选项-E。
#grep 'NW\|EA' testfile northwest NW Charles Main 3.0 .98 3 34 eastern EA TB Savage 4.4 .84 5 20
搜索全部包含一个或多个3的行。
# egrep '3+' testfile # grep -E '3+' testfile # grep '3\+' testfile #这3条命令将会 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
搜索全部包含0个或1个小数点字符的行。
# egrep '2\.?[0-9]' testfile # grep -E '2\.?[0-9]' testfile # grep '2\.\?[0-9]' testfile #首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。 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的行。
# egrep '(no)+' testfile # grep -E '(no)+' testfile # grep '\(no\)\+' testfile #3个命令返回相同结果, 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
不使用正则表达式
fgrep 查询速度比grep命令快,可是不够灵活:它只能找固定的文本,而不是规则表达式。
若是你想在一个文件或者输出中找到包含星号字符的行
fgrep '*' /etc/profile for i in /etc/profile.d/*.sh ; do 或 grep -F '*' /etc/profile for i in /etc/profile.d/*.sh ; do
参数总结:
◎参数 1. -A NUM,–after-context=NUM 除了列出符合行以外,而且列出后NUM行。 ex: $ grep -A 1 panda file (从file中搜寻有panda样式的行,并显示该行的后1行) 2. -a或–text grep本来是搜寻文字文件,若拿二进制的档案做为搜寻的目标, 则会显示以下的讯息: Binary file 二进制文件名 matches 而后结束。 若加上-a参数则可将二进制档案视为文本文件搜寻, 至关于–binary-files=text这个参数。 ex: (从二进制档案mv中去搜寻panda样式) (错误!!!) $ grep panda mv Binary file mv matches (这表示此档案有match之处,详见–binary-files=TYPE ) $ (正确!!!) $ grep -a panda mv 3. -B NUM,–before-context=NUM 与 -A NUM 相对,但这此参数是显示除符合行以外 并显示在它以前的NUM行。 ex: (从file中搜寻有panda样式的行,并显示该行的前1行) $ grep -B 1 panda file 4. -C [NUM], -NUM, –context[=NUM] 列出符合行以外并列出上下各NUM行,默认值是2。 ex: (列出file中除包含panda样式的行外并列出其上下2行) (若要改变默认值,直接改变NUM便可) $ grep -C[NUM] panda file 5. -b, –byte-offset 列出样式以前的内文总共有多少byte .. ex: $ grep -b panda file 显示结果相似于: 0:panda 66:pandahuang 123:panda03 6. –binary-files=TYPE 此参数TYPE预设为binary(二进制),若以普通方式搜寻,只有2种结果: 1.如有符合的地方:显示Binary file 二进制文件名 matches 2.若没有符合的地方:什么都没有显示。 若TYPE为without-match,遇到此参数, grep会认为此二进制档案没有包含任何搜寻样式,与-I 参数相同。 若TPYE为text, grep会将此二进制文件视为text档案,与-a 参数相同。 Warning: –binary-files=text 若输出为终端机,可能会产生一些没必要要的输出。 7. -c, –count 不显示符合样式行,只显示符合的总行数。 若再加上-v,–invert-match,参数显示不符合的总行数。 8. -d ACTION, –directories=ACTION 若输入的档案是一个资料夹,使用ACTION去处理这个资料夹。 预设ACTION是read(读取),也就是说此资料夹会被视为通常的档案; 若ACTION是skip(略过),资料夹会被grep略过: 若ACTION是recurse(递归),grep会去读取资料夹下全部的档案, 此至关于-r 参数。 9. -E, –extended-regexp 采用规则表示式去解释样式。 10. -e PATTERN, –regexp=PATTERN 把样式作为一个partern,一般用在避免partern用-开始。 11. -f FILE, –file=FILE 事先将要搜寻的样式写入到一个档案,一行一个样式。 而后采用档案搜寻。 空的档案表示没有要搜寻的样式,所以也就不会有任何符合。 ex: (newfile为搜寻样式文件) $grep -f newfile file 12. -G, –basic-regexp 将样式视为基本的规则表示式解释。(此为预设) 13. -H, –with-filename 在每一个符合样式行前加上符合的文件名称,如有路径会显示路径。 ex: (在file与testfile中搜寻panda样式) $grep -H panda file ./testfile file:panda ./testfile:panda $ 14. -h, –no-filename 与-H参数相相似,但在输出时不显示路径。 15. –help 产生简短的help讯息。 16. -I grep会强制认为此二进制档案没有包含任何搜寻样式, 与–binary-files=without-match参数相同。 ex: $ grep -I panda mv 17. -i, –ignore-case 忽略大小写,包含要搜寻的样式及被搜寻的档案。 ex: $ grep -i panda mv 18. -L, –files-without-match 不显示日常通常的输出结果,反而显示出没有符合的文件名称。 19. -l, –files-with-matches 不显示日常通常的输出结果,只显示符合的文件名称。 20. –mmap 若是可能,使用mmap系统呼叫去读取输入,而不是预设的read系统呼叫。 在某些情况,–mmap 能产生较好的效能。 然而,–mmap 若是运做中档案缩短,或I/O 错误发生时, 可能形成未定义的行为(包含core dump),。 21. -n, –line-number 在显示行前,标上行号。 ex: $ grep -n panda file 显示结果类似于下: 行号:符合行的内容 22. -q, –quiet, –silent 不显示任何的通常输出。请参阅-s或–no-messages 23. -r, –recursive 递归地,读取每一个资料夹下的全部档案,此至关于 -d recsuse 参数。 24. -s, –no-messages 不显示关于不存在或没法读取的错误讯息。 小注: 不像GNU grep,传统的grep不符合POSIX.2协议, 由于缺少-q参数,且他的-s 参数表现像GNU grep的 -q 参数。 Shell Script倾向将传统的grep移植,避开-q及-s参数, 且将输出限制到/dev/null。 POSIX: 定义UNIX及UNIX-like系统须要提供的功能。 25. -V, –version 显示出grep的版本号到标准错误。 当您在回报有关grep的bugs时,grep版本号是必需要包含在内的。 26. -v, –invert-match 显示除搜寻样式行以外的所有。 27. -w, –word-regexp 将搜寻样式视为一个字去搜寻,彻底符合该”字”的行才会被列出。 28. -x, –line-regexp 将搜寻样式视为一行去搜寻,彻底符合该”行”的行才会被列出。
文章出处:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html