`
thecloud
  • 浏览: 877636 次
文章分类
社区版块
存档分类
最新评论

如何使用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



分享到:
评论

相关推荐

    linux grep命令详解

    linux下grep的详细用法介绍,有需要的欢迎下载

    linux grep命令详解_linux_grep_

    linux grep 命令的详情解释 给初级者

    Linux Grep命令使用的详细介绍

    grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包 括grep、egrep...linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。

    linuxgrep命令.pdf

    linuxgrep命令.pdf

    unix的grep命令

    unix下学习grep命令不可多得的资料,决定原创,绝对值的你拥有!!

    Your.UNIX.Linux.The.Ultimate.Guide.3rd.Ed

    Your UNIX/Linux: The Ultimate Guide, written with both users and programmers in mind, is the ultimate UNIX/Linux text. Both pedagogical tool and exhaustive reference, it is well-suited to any course ...

    最全的linux下grep命令的使用方法

    grep命令大全记录了linux下grep命令的使用方法,很全面,很强大。可以当一个帮助文档使用。

    unix ed编辑器命令详解

    首先要学习的就是文本编辑,而 ed 编辑器是 unix/linux 操作系统下最简单、最典型的文本编辑器,因此,学习 ed 编辑器能够帮助你很好的理解一般的文本操作,而且还可以为你以后学习 unix/linux下的一些常用命令(如 ...

    Linux grep 命令详解

    含有详细的Grep命令,快速易懂

    linux匹配命令awk、grep、sed简介

    awk本身就是UNIX/Linux的一种编程语言,用于处理数据和生成报告。 awk逐行方式扫描文件(或输入),以查找匹配某个特定模式的文本行,并对这些文本行执行指定动作。 awk是三个创始人的首字母,代表三个作者:Alfred ...

    grep命令介绍.docx

    grep命令介绍.docx Unix/Linux

    linux的scp命令怎么用_linux的grep命令用法.docx

    linux的scp命令怎么用_linux的grep命令用法.docx

    Linux学习笔记【博文整理系列】

    Linux笔记——命令:grep Linux笔记——命令:find Linux笔记——命令:Sort,uniq,join,cut,paste,split Linux笔记——shell基础:变量&本地变量&位置变量&特定变量参数 Linux笔记——条件测试test Linux笔记——控制...

    linux中grep命令的使用.docx

    linux中grep命令的使用.docx

    Linux grep 命令用法详解

    Linux grep 命令 Linux grep 命令用于查找文件里符合条件的字符串。 grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显示出来...

    linux-shell脚本命令:grep命令简介

    本文主要对linux-shell脚本命令中的grep命令进行简介,希望对您有所帮助。

    详细unix命令大全

    查看UNIX内存和SWAP的命令 AIX: /usr/sbin/lsattr -El sys0 -a realmem /usr/sbin/lsps -s HP-UX: grep Physical /var/adm/syslog/syslog.log /usr/sbin/swapinfo -t LINUX: cat /proc/meminfo|grep MemTotal ...

    Linux Grep命令的详细使用方法

    关于grep命令的使用方法,以及该命令的参数,同时还有一些例子说明

Global site tag (gtag.js) - Google Analytics