如何使用Unix/Linux grep命令——磨刀不误砍柴工系列

如何使用Unix/Linux grep命令
——磨刀不误砍柴工系列
Garygaowork#gmail.com

grep在一个或多个文件中查找与模式字符串(pattern)匹配的行,并将搜索的结果打印出来,不会修改原文件内容。
使用grep命令的语法为:

$grep[option(s)]pattern[file(s)]

其中optiongrep命令的选项,pattern为要匹配的简单字符串或携带特殊字符的模式字符串,file为文件列表,可有多个文件。



Part 1 grep中经常用到的选项(option)

-i 忽略pattern中的大小写
$grep -i hAL /etc/passwd
-w 搜索整个词汇
忽略大小写并搜索整个词汇"samba"
 $grep -iw "samba" /tec/samba/smb.conf 
 # This is the main Samba configuration file. You should read the 
 # here. Samba has a huge number of configurable options (perhaps too 
 # For a step to step guide on installing, configuring and using samba, 
 # read the Samba-HOWTO-Collection. This may be obtained from:



-r 递归地指定文件所在目录中的所有子目录中的文件
-v 查找与pattern不匹配的行
定义输出方式:
-o 仅打印出匹配的一段,而非整行
-n 打印出匹配行的行号
-l 仅打印出匹配行所在的文件
-c 打印出每个文件中匹配行的总数
-A num 显示匹配行之后的num行
-B num 显示匹配行之前的num行
-C num 相当于 -A num 与 -B num 的组合
--color=auto 将pattern在匹配行中高亮输出

注意:
(1).选项区分大小写
(2).多个选项可以一起使用,例如:
 $grep -iwr
(3).grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。

Part 2 在grep中使用正则表达式

1.基本的匹配模式
尽管直接使用最简单直接的pattern字串可以完成一些重要任务,但是grep命令的真正威力在于它可以使用正则表达式来完成复杂的模式字串的匹配。grep命令中使用的是“基本的正则表达式”,如果想使用更高级的正则表达式规则,需要指定选项 -E ,相当于egrep命令。
以下字符或字符串在正则表达式的规则中具有特殊意义,如,*,+,[,],^,$,\,{,}
它们的多种组合展示了基本的正则表达式的匹配模式:
(1)'.'匹配任意单一字符
(2) X* 与包含连续0个或多个字符X的行匹配
(3) X\+ 与包含连续1个或多个字符X的行匹配
(4) [a-z] 与包含a-z的其中一个字符的行匹配
(5) [^a-z] 与不包含a-z的其中一个字符的行匹配
(6) [0-9] 与包含0-9的其中一个字符的行匹配
(7) ^hello 与以字串hello起始的行匹配
(8) hello$ 与以字串hello结束的行匹配

(9) \ 转义字符,后跟特殊字符,可表示它本来的涵义

(10)

\d 匹配一个数字字符. 等价于 [0-9]
\D 匹配一个非数字符. 等价于 [^0-9]
\w ,等价于 "[A-Za-z0-9_]"
\W 匹配任何非单词字符,等价于 "[^A-Za-z0-9]"
\s 匹配任何空白字符, 包括空格 制表符 换页符 等等. 等价于[\f\n\r\t\v]
\S 匹配任何非空白字符. 等价于 [^\f\r\n\t\v]
\b 匹配一个单词边界,也就是指单词和空格间的位置。
\B 匹配非单词边界。

如,

 $grep '[' filename

返回结果为grep : Invalid regular expression

而 ,

 $grep '\[' filename
会匹配所有包含'['(不包括单引号)的行。

X\{n\} 与连续包含n个字符X的行匹配
(11) X\ {n,\} 与至少连续包含n个字符X的行匹配 (注意n后面的',')
(12) X \{,m\} 与最多连续包含m个字符X的行匹配 (注意m前面的',')
(13) X \{n,m\} 与最少包含n个,最多包含m个字符X的行匹配

注意:

1.不要混淆shell 中的".","*"与正则表达式中的".","*",很多刚开始学的人都会犯错。
在正则表达式中,"."很像shell中的"?",它与任意单一字符匹配。而"*"在正则表达式中的使用,表示"*"前面的字符可能出现0次或1次或多次,与shell中的"*"涵义不同。

2.grep 中模式(pattern)之间的OR,AND,NOT 操作
1.grep or 操作(4 种方法)
1.1 使用 \|

 $grep 'pattern1\|pattern2' filename 
1.2.使用 -E 选项
grep -E 代表扩展的正则表达式. 若使用-E选项,则可以去掉转义字符'\',直接使用'|'

 $grep -E 'pattern1|pattern2' filename 
1.3.使用 egrep 命令
egrep 相当于 ‘grep -E’.
1.4.使用 -e 选项
通过指定多个-e选项来应用多个pattern,多个pattern之间是“或”的关系
$grep -e pattern1 -e pattern2 filename


2.grep AND 操作
2.1 使用 -E选项和模式字符 'pattern1.*pattern2'

$grep -E 'pattern1.*pattern2' filename 

以上命令为在filename文件中查找既与pattern1匹配又与pattern2匹配的行

$grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename 

2.2 利用管道实现

$grep -E 'pattern1' filename | grep -E 'pattern2' 
3.Grep NOT 操作
3.1使用 -v 选项

$grep -v 'pattern' filename 
以上命令在filename文件中查找不能与pattern匹配的行




Part 3 grep命令的 Examples

1. 对文件中的空行计数

 $grep -c "^$" filename 
2.查找在“hello”有任意长度字串的行

 $grep ".*hello" filename 

3.查找在'hi'与'hello'之间至少有一个空格的行

 $grep "hi \+hello" filename
4.查找在'hi'与'hello'之间没有空格或有多个空格的行

$grep "hi *hello" filename 
5..查找在'hi'与'hello'之间没有空格或有1个空格的行

 $grep "hi \?hello" filename 
6.查找ip地址127.0.0.1 (其中包含特殊字符 '.')

 $grep "127\.0\.0\.1" filename
7.过滤其他命令的输出结果

 $ls --help | grep "dired"
将ls命令的帮助文本,作为grep的输入,查找与"dired"匹配的行
输出为:-D, --dired generate output designed for Emacs' dired mode
8.从系统日志中获得有用的信息
在apache日志文件中查找以IP地址开头,包含数字200的行

 $grep -Eoc "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.* 200" /srv/www/example.com/logs/access.log


References:

[1]How To Use Linux Grep Command To Find Strings
http://www.expertslogin.com/linux-administration/linux-grep-command-find-strings/
[2]Grep command in Linux explained Guide Discover grep and never lose anything again
http://www.techradar.com/news/software/operating-systems/grep-command-in-linux-explained-699455
[3] Search and Filter Text with Grep http://library.linode.com/linux-tools/common-commands/grep
[4] Linux / Unix Command: grep http://linux.about.com/od/commands/l/blcmdl1_grep.htm
[5] Regular Expressions in Grep Command with 10 Examples
http://www.thegeekstuff.com/2011/01/regular-expressions-in-grep-command/
[6] Using grep http://www.linuxjournal.com/article/1149?page=0,1
[7]15 Practical Grep Command Examples In Linux / UNIX (很丰富实用的举例)
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
(The Geek Stuff 是个很不错的linux博客)
[8]7 Linux Grep OR, Grep AND, Grep NOT Operator Examples
http://www.thegeekstuff.com/2011/10/grep-or-and-not-operators/
[9]HowTo:Use Grep Command in Linux/Unix[examples]
http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
[10]Advanced Regular Expressions in Grep Command with 10 Examples – Part II
(更高级别的正则表达式)
http://www.thegeekstuff.com/2011/01/advanced-regular-expressions-in-grep-command-with-10-examples-%E2%80%93-part-ii/
[11]详细介绍Linux grep指令 http://os.51cto.com/art/200912/168882.htm
[12] 关于Linux Grep命令使用的详细介绍
http://fanqiang.chinaunix.net/system/linux/2007-03-15/5110.shtml



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值