linuxgrep【正则搜索文本】

http://man.chinaunix.net/newsoft/grep/open.htm

1 grep简介

grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包 括grepegrepfgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的re元字符fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能 更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。

grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到屏幕,不影响原文件内容。

grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。

【特别注意】

1,grep用正则匹配进行文本搜索

2,grep搜索建议:

        字符串建议使用双引号:grep "ZOO_INFO" zookeeper.log

       正则匹配应使用单引号:grep "\<ZOO_INFO\>" zookeeper.log

        用法:grep [options] PATTERN [FILE...]

2 grep正则表达式元字符集(基本集)

[plain] view plaincopy
 
  1. ^     锚定行的开始 如:'^grep'匹配所有以grep开头的行。  
  2. $    锚定行的结束 如:'grep$'匹配所有以grep结尾的行。  
  3. .    匹配一个非换行符的字符 如:'gr.p'匹配例如grep。  
  4. *    匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。 .*一起用代表任意字符。  
  5. []   匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。  
  6. [^]  匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-F和H-Z的一个字母开头,紧跟rep的行。  
  7. \(..\) 标记匹配字符,如'\(love\)',love被标记为1。  
  8. \<   锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。  
  9. \>   锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。  
  10. x\{m\}  重复字符x,m次,如:'o\{5\}'匹配包含5个o的行。  
  11. x\{m,\}  重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。  
  12. x\{m,n\} 重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。  
  13. \w   匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。  
  14. \W     \w的反置形式,匹配一个或多个非单词字符,如点号句号等。  
  15. \b  单词锁定符,如: '\bgrep\b'只匹配grep。  

3. 用于egrep和 grep -E的元字符扩展集

[plain] view plaincopy
 
  1. +      匹配一个或多个先前的字符。如:'[a-z]+able',匹配一个或多个小写字母后跟able的串,如loveable,enable,disable等。  
  2. ?      匹配零个或多个先前的字符。如:'gr?p'匹配gr后跟一个或没有字符,然后是p的行。  
  3. a|b|c  匹配a或b或c。如:grep|sed匹配grep或sed  
  4. ()     分组符号,如:love(able|rs)ov+匹配loveable或lovers,匹配一个或多个ov。  
  5. x{m},x{m,},x{m,n}    作用同x\{m\},x\{m,\},x\{m,n\}  

4. POSIX字符类

为 了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:]是A-Za-z0-9的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。

[plain] view plaincopy
 
  1. [:alnum:]   文字数字字符  
  2. [:alpha:]   文字字符  
  3. [:digit:]   数字字符  
  4. [:graph:]   非空字符(非空格、控制字符)  
  5. [:lower:]   小写字符  
  6. [:cntrl:]   控制字符  
  7. [:print:]   非空字符(包括空格)  
  8. [:punct:]   标点符号  
  9. [:space:]   所有空白字符(新行,空格,制表符)  
  10. [:upper:]   大写字符  
  11. [:xdigit:]  十六进制数字(0-9,a-f,A-F)  

5. Grep命令选项

[plain] view plaincopy
 
  1. -?  
  2. 同时显示匹配行上下的?行,如:grep -2 pattern filename同时显示匹配行的上下2行。  
  3. -b,--byte-offset  
  4. 打印匹配行前面打印该行所在的块号码。  
  5. -c,--count  
  6. 只打印匹配的行数,不显示匹配的内容。  
  7. -f File,--file=File  
  8. 从文件中提取模板。空文件中包含0个模板,所以什么都不匹配。  
  9. -h,--no-filename  
  10. 当搜索多个文件时,不显示匹配文件名前缀。  
  11. -i,--ignore-case  
  12. 忽略大小写差别。  
  13. -q,--quiet  
  14. 取消显示,只返回退出状态。0则表示找到了匹配的行。  
  15. -l,--files-with-matches  
  16. 打印匹配模板的文件清单。  
  17. -L,--files-without-match  
  18. 打印不匹配模板的文件清单。  
  19. -n,--line-number  
  20. 在匹配的行前面打印行号。  
  21. -s,--silent  
  22. 不显示关于不存在或者无法读取文件的错误信息。  
  23. -v,--revert-match  
  24. 反检索,只显示不匹配的行。  
  25. -w,--word-regexp  
  26. 类似正则中被\<和\>引用,就把表达式做为一个单词搜索。只匹配整个单词,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’)。  
  27. -V,--version  
  28. 显示软件版本信息。  
  29. -r,-R, --recursive  
  30. 递归查找  


6. 实例

要用好grep这个工具,其实就是要写好正则表达式,所以这里不对grep的所有功能进行实例讲解,只列几个例子,讲解一个正则表达式的写法。

$ ls -l | grep '^d'
通过管道过滤ls -l输出的内容,只显示以a开头的行。

$ grep 'test' d*
显示所有以d开头的文件中包含test的行。

$ grep 'test' aa bb cc
显示在aa,bb,cc文件中匹配test的行。

$ grep '[a-z]\{5\}' aa
显示所有包含每个字符串至少有5个连续小写字符的字符串的行。

$ grep 'w\(es\)t.*\1' aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),这些字符后面紧跟着另外一个es(\1),找到就显示该行。如果用egrep或grep -E,就不用"\"号进行转义,直接写成'w(es)t.*\1'就可以了。

7. 练习grep命令

demo_file

[plain] view plaincopy
 
  1. THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.  
  2. this line is the 1st lower case line in this file.  
  3. This Line Has All Its First Character Of The Word With Upper Case.  
  4.   
  5. Two lines above this line is empty.  
  6. And this is the last line.  

7.1.从单个文件中搜索指定的字串

grep的基础用法是如下例的从指定的文件中搜索特定的字串。
语法:
grep "literal_string" filename

[plain] view plaincopy
 
  1. $ grep "this" demo_file  
  2. this line is the 1st lower case line in this file.  
  3. Two lines above this line is empty.  
  4. And this is the last line.  

7.2. 在多个文件中检索指定的字串

语法:
grep "string" FILE_PATTERN

先拷贝demo_file为demo_file1。grep的结果在符合条件的行前将包括文件名。当文件名包含元字符时,linux shell会将匹配的所有文件作为输入到grep中去。

[plain] view plaincopy
 
  1. $ cp demo_file demo_file1  
  2. $ grep "this" demo_*  
  3. demo_file:this line is the 1st lower case line in this file.  
  4. demo_file:Two lines above this line is empty.  
  5. demo_file:And this is the last line.  
  6. demo_file1:this line is the 1st lower case line in this file.  
  7. demo_file1:Two lines above this line is empty.  
  8. demo_file1:And this is the last line.  

7.3. 用 grep -i 进行大小写无关的搜索

语法:
grep -i "string" FILE
也是一个基本用法,对搜索的字串忽略大小写,因此下例中匹配“the”, “THE” and “The”。

[plain] view plaincopy
 
  1. $ grep -i "the" demo_file  
  2. THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.  
  3. this line is the 1st lower case line in this file.  
  4. This Line Has All Its First Character Of The Word With Upper Case.  
  5. And this is the last line.  

7.4. 使用用正则表达式

语法:
grep "REGEX" filename
如果你能有效地利用正则表达式,这是个很有用的特点。在下面的例子中,搜索全部以“lines”开始以“empty”结束的字串,如搜索“lines[之间任意字]empty” ,并且忽略大小写。

[plain] view plaincopy
 
  1. $ grep -i "lines.*empty" demo_file  
  2. Two lines above this line is empty.  

7.5. 用grep -w搜索整个词,而不是词中的部分字串

使用-w选项搜索一个单词,并且避免搜索到词中的部分字串。
下例搜索"is"。如果不加-w选项,将显示“is”, “his”, “this” 等所有包含“is”的行。

[plain] view plaincopy
 
  1. $ grep -i "is" demo_file  
  2. THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.  
  3. this line is the 1st lower case line in this file.  
  4. This Line Has All Its First Character Of The Word With Upper Case.  
  5. Two lines above this line is empty.  
  6. And this is the last line.  

下例使用了-w选项,请注意结果中不包含 “This Line Has All Its First Character Of The Word With Upper Case”, 虽然 “This”中包含“is”。

[plain] view plaincopy
 
  1. $ grep -iw "is" demo_file  
  2. THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.  
  3. this line is the 1st lower case line in this file.  
  4. Two lines above this line is empty.  
  5. And this is the last line.  

7.6. 使用grep -A, -B and -C显示之前、之后、前后的几行

当使用grep搜索大文件时,显示匹配行附近的多行数据是一个很有用的功能。

7.6.1 显示匹配行之后的N行
-A
语法:
grep -A  "string" FILENAME
下例显示匹配行和之后的3行数据

[plain] view plaincopy
 
  1. $ grep -A 3 -i "example" demo_text  


7.6.2显示匹配行之前的N行
-B
语法:
grep -B  "string" FILENAME
下例显示匹配行和之前的2行数据

[plain] view plaincopy
 
  1. $ grep -B 2 "single WORD" demo_text  


7.6.3显示匹配行前后的N行
-C 显示之前的n行,之后的n行数据.

[plain] view plaincopy
 
  1. $ grep -C 2 "Example" demo_text  

7.7.通过GREP_OPTIONS高亮显示搜索的字串

如果你希望搜索的字串高亮显示在结果中,可以试用以下的办法。通过修改GREP_OPTIONS对搜索字串高亮显示。

[plain] view plaincopy
 
  1. $ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'  

7.8. 用grep -r递归搜索全部的文件

如果想查找当前目前以及其子目录的全部文件时,可以使用 -r 选项。如下例

[plain] view plaincopy
 
  1. $ grep -r "ramesh" *  

7.9. 使用grep -v进行不匹配

可以使用-v选项显示不匹配搜索字串的行。

下例显示demo_text文件中不包含“go”的行

[plain] view plaincopy
 
  1. $ grep -v "go" demo_text  

7.10. 显示不匹配全部模式的行

语法:
grep -v -e "pattern" -e "pattern"
创建如下例子文件test-file.txt

[plain] view plaincopy
 
  1. a  
  2. b  
  3. c  
  4. d  
[plain] view plaincopy
 
  1. $ grep -v -e "a" -e "b" -e "c" test-file.txt  
  2. d  

7.11.用grep -c 统计匹配的行数

语法:
grep -c "pattern" filename

[plain] view plaincopy
 
  1. $ grep -c "go" demo_text  
  2. 6  

统计不匹配的行数

[plain] view plaincopy
 
  1. $ grep -v -c this demo_file  
  2. 4  



7.12. 用grep -l 只显示文件名

[plain] view plaincopy
 
  1. $ grep -l this demo_*  
  2. demo_file  
  3. demo_file1  

7.13. 只显示匹配的字串

缺省显示匹配字串的所在行,可以使用-o选项只显示匹配的字串。这项功能当使用正则表达式时比较有用处。

[plain] view plaincopy
 
  1. $ grep -o "is.*line" demo_file  
  2. is line is the 1st lower case line  
  3. is line  
  4. is is the last line  



7.14. 显示匹配的位置

语法:
grep -o -b "pattern" file

[plain] view plaincopy
 
  1. $ cat temp-file.txt  
  2. 12345  
  3. 12345  
[plain] view plaincopy
 
  1. $ grep -o -b "3" temp-file.txt  
  2. 0:3  
  3. 6:3  

注意: 以上输出显示的不是行内的位置,而是整个文件中的字节byte位置

7.15. 用 grep -n 在输出时显示行号

行号从1开始

[plain] view plaincopy
 
    1. $ grep -n "go" demo_text  
原文地址:https://www.cnblogs.com/wickedboy237/p/3095378.html