首页 > 系统服务 > 详细

linux grep命令总结

时间:2017-04-06 17:25:13      阅读:216      评论:0      收藏:0      [点我收藏+]

linux grep命令总结

简介

grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

grep常用用法

[root@www ~]# grep [-acinv] [--color=auto] ‘搜寻字符串‘ filename

选项与参数:

-a :将 binary 文件以 text 文件的方式搜寻数据

-c :计算找到 ‘搜寻字符串‘ 的次数

-i :忽略大小写的不同,所以大小写视为相同

-n :顺便输出行号

-s 不显示错误信息
-E 使用扩展正则表达式

-v :反向选择,亦即显示出没有 ‘搜寻字符串‘ 内容的那一行!

--color=auto :可以将找到的关键词部分加上颜色的显示喔!

pattern正则表达式主要参数:
\: 忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
\<:从匹配正则表达 式的行开始。
\>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的单个字符。
* :有字符,长度可以为0。

\< 和 \> 分别标注单词的开始与结尾。
例如:
grep man * 会匹配 ‘Batman’、’manic’、’man’等,
grep ‘\<man’ * 匹配’manic’和’man’,但不是’Batman’,
grep ‘\<man\>’ 只匹配’man’,而不是’Batman’或’manic’等其他的字符串。

将/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

可以使用国际模式匹配的类名:

[[:upper:]]   [A-Z]
[[:lower:]]   [a-z]
[[:digit:]]   [0-9]
[[:alnum:]]   [0-9a-zA-Z]
[[:space:]]   空格或tab
[[:alpha:]]   [a-zA-Z]

将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号

# grep -n root /etc/passwd
3:root:x:0:0:root:/root:/bin/bash
5:operator:x:11:0:operator:/root:/sbin/nologin

grep 可以使用 --color=auto 来将关键字部分使用颜色显示。你可以在 ~/.bashrc 内加上这行:『alias grep=‘grep --color=auto‘』再以『 source ~/.bashrc 』来立即生效。

将/etc/passwd,将root行过滤掉,不出现带有root字符的行。

[root@www script]# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
将带有root的字符已经过滤掉了。

将/etc/passwd中,过滤掉root 和nologin,不出现带有root 和nologin字符的行。

[root@www script]# grep -v root /etc/passwd|grep -v nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
将带有root 和nologin的字已经过滤掉了。

根据文件内容递归查找目录

# grep ‘mysql’ *           #在当前目录搜索带‘energywise‘行的文件

# grep -r ‘mysql’ *        #在当前目录及其子目录下搜索‘energywise‘行的文件
# grep -l -r ‘mysql’ *     #在当前目录及其子目录下搜索‘energywise‘行的文件,但是不显示匹配的行,只显示匹配的文件

grep与正规表达式

[root@www script]# grep "google\>" regular.txt 

.google is the best tools for search keyword.

[root@www script]# grep "\<google" regular.txt  

.google is the best tools for search keyword.

[root@www script]# grep "\<goog" regular.txt   

.google is the best tools for search keyword.

[root@www script]# grep "\<goo" regular.txt              模糊匹配关键字

Oh! The soup taste good.

.google is the best tools for search keyword.

gooogle yes!

[root@www script]# grep "goo\>" regular.txt              没有匹配到关键字

[root@www script]# grep "google\>" regular.txt 

.google is the best tools for search keyword.

[root@www script]# grep "\<google\>" regular.txt      精确匹配关键字

.google is the best tools for search keyword.

[root@www script]# grep "\<google" regular.txt         精确匹配关键字

.google is the best tools for search keyword.

[root@www script]# grep "google" regular.txt             精确匹配关键字

.google is the best tools for search keyword.

[root@www script]# grep "\<[Gg]oogle" regular.txt   

google is the best tools for search keyword.

Google is the best tools for search keyword.


字符类

字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 ‘t?st‘ 存在~这个时候,我可以这样来搜寻:

[root@www ~]# grep -n ‘t[ae]st‘ regular.txt 

5:I can‘t finish the test.

7:Oh! The soup taste good.

字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下

[root@www ~]# grep -n ‘[^g]oo‘ regular.txt 

2:apple is my favorite food.

3:Football game is not use feet only.

9:google is the best tools for search keyword.

11:goooooogle yes!

第 2,3 行没有疑问,因为 foo 与 Foo 均可被接受,但是第 9 行明明有 google 的 goo 啊,别忘记了,因为该行后面出现了 tool 的 too 啊!所以该行也被列出来,也就是说, 9 行里面虽然出现了我们所不要的项目 (goo) 但是由於有需要的项目 (too) , 因此是符合字串搜寻的。

至于第 11 行,同样的,因为 goooooogle 里面的 oo 前面可能是 o ,例如: go(ooo)oogle ,所以,这一行也是符合需求的。

假设我 oo 前面不想要有小写字母,所以我可以这样写 [^abcd....z]oo,但是这样并不方便,由于小写字母的 ASCII 上编码的顺序是连续的, 因此,我们可以这样来写:

[root@www ~]# grep -n ‘[^a-z]oo‘ regular.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.txt 
5:However, this dress is about $ 3183 dollars.
8: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.

如果我不想要开头是英文字母,则可以是这样:

[root@www ~]# grep -n ‘^[^a-zA-Z]‘ regular.txt
1:"Open Source" is a good mechanism to develop programs.
11:# I am VBird

注意:因为小数点具有其他意义,所以必须要使用转义字符(\)来加以解除其特殊意义!

找出空白行:

[root@www ~]# grep -n ‘^$‘ regular_express.txt
5:

因为只有行首跟行尾 (^$),所以,这样就可以找出空白行啦!

本文出自 “每天一小步” 博客,请务必保留此出处http://fenyuer.blog.51cto.com/11265169/1913332

linux grep命令总结

原文:http://fenyuer.blog.51cto.com/11265169/1913332

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!