Grep学习笔记

 

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

1. grep简介

grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep、egrep和fgrep。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。我们利用这些返回值就可进行一些自动化的文本处理工作。

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

^

锚定行的开始 如:'^grep'匹配所有以grep开头的行。

$

锚定行的结束 如:'grep$'匹配所有以grep结尾的行。

.

匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。

*

匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。 .*一起用代表任意字符。

[]

匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。

[^]

匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。

\(..\)

标记匹配字符,如'\(love\)',love被标记为1。

\<

锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。

\>

锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。

x\{m\}

重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。

x\{m,\}

重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。

x\{m,n\}

重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。

\w

匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。

\W

\w的反置形式,匹配一个或多个非单词字符,如点号句号等。

\b

单词锁定符,如: '\bgrep\b'只匹配grep。

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

+

匹配一个或多个先前的字符。如:'[a-z]+able',匹配一个或多个小写字母后跟able的串,如loveable,enable,disable等。

?

匹配零个或多个先前的字符。如:'gr?p'匹配gr后跟一个或没有字符,然后是p的行。

a|b|c

匹配a或b或c。如:grep|sed匹配grep或sed

()

分组符号,如:love(able|rs)ov+匹配loveable或lovers,匹配一个或多个ov。

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的字符类。

[:alnum:]

文字数字字符

[:alpha:]

文字字符

[:digit:]

数字字符

[:graph:]

非空字符(非空格、控制字符)

[:lower:]

小写字符

[:cntrl:]

控制字符

[:print:]

非空字符(包括空格)

[:punct:]

标点符号

[:space:]

所有空白字符(新行,空格,制表符)

[:upper:]

大写字符

[:xdigit:]

十六进制数字(0-9,a-f,A-F)

5. Grep命令选项

-?

同时显示匹配行上下的?行,如:grep -2 pattern filename同时显示匹配行的上下2行。

-b,--byte-offset

打印匹配行前面打印该行所在的块号码。

-c,--count

只打印匹配的行数,不显示匹配的内容。

-f File,--file=File

从文件中提取模板。空文件中包含0个模板,所以什么都不匹配。

-h,--no-filename

当搜索多个文件时,不显示匹配文件名前缀。

-i,--ignore-case

忽略大小写差别。

-q,--quiet

取消显示,只返回退出状态。0则表示找到了匹配的行。

-l,--files-with-matches

打印匹配模板的文件清单。

-L,--files-without-match

打印不匹配模板的文件清单。

-n,--line-number

在匹配的行前面打印行号。

-s,--silent

不显示关于不存在或者无法读取文件的错误信息。

-v,--revert-match

反检索,只显示不匹配的行。

-w,--word-regexp

如果被\<和\>引用,就把表达式做为一个单词搜索。

-V,--version

显示软件版本信息。

6. 实例

 

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

$ ls -l | grep '^a'

通过管道过滤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'就可以了。

 

linux grep命令 -w -R参数介绍

 

 

linux grep命令中的-w和-R或-r参数。这两个参数在一般教程中很少提到,但在特定的时候用处很大。

-w, –word-regexp
Select only those  lines  containing  matches  that  form  whole
words.   The  test is that the matching substring must either be
at the beginning of the line, or preceded  by  a  non-word  con-
stituent  character.  Similarly, it must be either at the end of
the line or followed by a non-word constituent character.  Word-
constituent  characters are letters, digits, and the underscore.

//意思就是精确匹配,匹配单词还不是字符串,如想匹配“is”,”this”就不会被匹配。实例如下:

 

[root@GoGo ~]# cat goface.txt

goface

gofaceme

[root@GoGo ~]# grep ’goface’ goface.txt

goface

gofaceme

[root@GoGo ~]# grep -w ’goface’ goface.txt

goface

怎么样,有实例一下子就看明白了吧。

-R, -r, –recursive
Read all files under each directory, recursively; this is equiv-
alent to the -d recurse option.

//-r/-R选项是递归搜索, 查找你搜索的目录或子目录下的所有含有某个你要找的文件.

[root@GoGo ~]# grep -R ’goface’ /root

/root/goface.txt:goface

/root/goface.txt:gofaceme

遍历/root下所有文件匹配goface。

如果寻找的目录下文件太多或含有大文件,可使用以下参数或结合find命令。

–include=PATTERN
Recurse in directories only searching file matching PATTERN.

–exclude=PATTERN
Recurse in directories skip file matching PATTERN.

相关阅读:

gnu grep manual:http://www.gnu.org/s/grep/manual/grep.html

linux find命令实例:http://blog.51osos.com/linux/linux-find-command/

linux grep命令参数:http://bbs.51osos.com/thread-3937-1-1.html

linux grep命令实例:http://bbs.51osos.com/thread-533-1-1.html

 

 

 

grep的用法

http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/ 

首先创建我们练习grep命令时需要用到的demo文件demo_file。

$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.
And this is the last line.

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

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

语法:
grep "literal_string" filename

 

$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

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

语法:
grep "string" FILE_PATTERN


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

 

$ cp demo_file demo_file1

$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.

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

语法:
grep -i "string" FILE


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

 

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

4. 使用用正则表达式

语法:
grep "REGEX" filename


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

 

$ grep -i "lines.*empty" demo_file
Two lines above this line is empty.

 

正则表达式遵循的几个重复的操作

  • ? 最多匹配一次
  • * 匹配零次或者任意多次
  • + 匹配一次以上
  • {n} 匹配n次
  • {n,} 最少匹配n次
  • {,m} 最多匹配m次
  • {n,m} 匹配n到m次

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

使用-w选项搜索一个单词,并且避免搜索到词中的部分字串。

下例搜索"is"。如果不加-w选项,将显示“is”, “his”, “this” 等所有包含“is”的行。

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


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

$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

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

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


创建如下文件

$ cat demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

 * e - go to the end of the current word.
 * E - go to the end of the current WORD.
 * b - go to the previous (before) word.
 * B - go to the previous (before) WORD.
 * w - go to the next word.
 * W - go to the next WORD.

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

 * 192.168.1.1 - single WORD
 * 192.168.1.1 - seven words.
6.1 显示匹配行之后的N行

-A

语法:
grep -A  "string" FILENAME


下例显示匹配行和之后的3行数据

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.2显示匹配行之前的N行

-B

语法:
grep -B  "string" FILENAME


下例显示匹配行和之前的2行数据

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
6.3显示匹配行前后的N行

-C 显示之前的n行,之后的n行数据.

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

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

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

通过修改GREP_OPTIONS对搜索字串高亮显示。

 

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

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

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

$ grep -r "ramesh" *

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

可以使用-v选项显示不匹配搜索字串的行。下例显示demo_text文件中不包含“go”的行

$ grep -v "go" demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

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

语法:
grep -v -e "pattern" -e "pattern"

 

创建如下例子文件

$ cat test-file.txt
a
b
c
d

$ grep -v -e "a" -e "b" -e "c" test-file.txt
d

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

语法:
grep -c "pattern" filename

 

$ grep -c "go" demo_text
6


统计不匹配的行数

$ grep -v -c this demo_file
4

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

$ grep -l this demo_*
demo_file
demo_file1

13. 只显示匹配的字串

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

$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line

14. 显示匹配的位置

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

 

$ cat temp-file.txt
12345
12345

$ grep -o -b "3" temp-file.txt
0:3
6:3


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

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

行号从1开始

$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值