grep的用法

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.
 
 
转: 网友总结的grep使用例子

正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串。vim、grep、awk 、sed 都支持正则表达式,也正是因为由于它们支持正则,才显得它们强大;在以前上班的公司里,由于公司是基于web的服务型网站(nginx),对正则的需求比 较大,所以也花了点时间研究正则,特与大家分享下:

1基础正则表达式 grep 工具,以前介绍过。 grep -[acinv] '搜索内容串' filename -a 以文本文件方式搜索 -c 计算找到的符合行的次数 -i 忽略大小写 -n 顺便输出行号 -v 反向选择,即找 没有搜索字符串的行 其中搜索串可以是正则表达式!

1 搜索有the的行,并输出行号 $grep -n 'the' regular_express.txt 搜 索没有the的行,并输出行号 $grep -nv 'the' regular_express.txt

2 利 用[]搜索集合字符 [] 表示其中的某一个字符 ,例如[ade] 表示a或d或e woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt 8:I can't finish the test. 9:Oh! the soup taste good!

可以用^符号做[]内的前缀,表示除[]内的字符之外的字 符。 比如搜索oo前没有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串 woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt 2:apple is my favorite food. 3:Football game is not use feet only. 18:google is the best tools for search keyword. 19:goooooogle yes!

[] 内可以用范围表示,比如[a-z] 表示小写字母,[0-9] 表示0~9的数字, [A-Z] 则是大写字母们。[a-zA-Z0-9]表示所有数字与英文字符。 当然也可以配合^来排除字符。 搜索包含数字的行 woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt 5:However ,this dress is about $ 3183 dollars. 15:You are the best is menu you are the no.1.

行首与行尾字符 ^ $. ^ 表示行的开头,$表示行的结尾( 不是字符,是位置)那么‘^$' 就表示空行,因为只有 行首和行尾。 这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。 比如搜索the在开头的行 woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt 12:the symbol '*' is represented as star.

搜索以小写字母开头的行 woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt 2:apple is my favorite food. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 12:the symbol '*' is represented as star. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go. woody@xiaoc:~/tmp$

搜索开头不是英文字母的行 woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 21:#I am VBird woody@xiaoc:~/tmp$

$表示它前面的串是在行的结尾,比如 '\.' 表示 . 在一行的结尾 搜索末尾是.的行 woody@xiaoc:~/tmp$ grep -n '\.$' regular_express.txt //. 是正则表达式的特殊符号,所以要用\转义 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn't fit me. 5:However ,this dress is about $ 3183 dollars. 6:GNU is free air not free beer. .....

注意在MS的系统下生成的文本文件,换行会加上一个 ^M 字符。所以最后的字符会是隐藏的^M ,在处理Windows 下面的文本时要特别注意! 可以用cat dos_file | tr -d '\r' > unix_file 来删除^M符号。 ^M==\r

那么'^$' 就表示只有行首行尾的空行拉! 搜索空行 woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt 22: 23: woody@xiaoc:~/tmp$

搜索非空行 woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn't fit me. ..........

任意一个字符. 与重复字符 *

在bash中*代表通配符,用来代表任意个 字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。 例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.

点. 代表一个任意字符,必须存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。

woody@xiaoc:~/tmp$ grep -n 'g..d' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 9:Oh! the soup taste good! 16:The world is the same with 'glad'. woody@xiaoc:~/tmp$

搜索两个o以上的字符串 woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前两个o一定存在,第三个o可没有,也可有多个。 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! the soup taste good! 18:google is the best tools for search keyword. 19:goooooogle yes!

搜索g开头和结尾,中间是至少一个o的字符串,即gog, goog....gooog...等 woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!

搜索g开头和结尾的字符串在的行 woody@xiaoc:~/tmp$ grep -n 'g.*g' regular_express.txt // .*表示 0个或多个任意字符 1:"Open Source" is a good mechanism to develop programs. 14:The gd software is a library for drafting programs. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go.

限定连续重复字符的范围 { } . * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用{范围} 。范围是数字用,隔开 2,5 表示2~5个, 2表示2个,2, 表示2到更多个 注意,由于{ }在SHELL中有特殊意义,因此作为正则表达式用的时候要用\转义一下。

搜索包含两个o的字符串的行。 woody@xiaoc:~/tmp$ grep -n 'o\{2\}' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! the soup taste good! 18:google is the best tools for search keyword. 19:goooooogle yes!

搜索g后面跟2~5个o,后面再跟一个g的字符串的行。 woody@xiaoc:~/tmp$ grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.

搜索包含g后面跟2个以上o,后面再跟g的行。。 woody@xiaoc:~/tmp$ grep -n 'go\{2,\}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!

注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。 '[^a-z\.!^ -]' 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意[]里面有个小空格。

另外shell 里面的反向选择为[!range], 正则里面是 [^range]

2扩展正则表达式

扩展正则表达式是对基础正则表达式添加了几个特殊构成的。 它令某些操作更加方便。 比如我们要去除 空白行和行首为 #的行, 会这样用: woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#' "Open Source" is a good mechanism to develop programs. apple is my favorite food. Football game is not use feet only. this dress doesn't fit me. ............

然而使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。 注意grep只支持基础表达式, 而egrep 支持扩展的, 其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。 那么: woody@xiaoc:~/tmp$ egrep -v '^$|^#' regular_express.txt "Open Source" is a good mechanism to develop programs. apple is my favorite food. Football game is not use feet only. this dress doesn't fit me. .................... 这里| 表示或的关系。 即满足 ^$ 或者 ^# 的字符串。

这里列出几个扩展特殊符号: +, 于 . * 作用类似,表示 一个或多个重复字符。 ?, 于 . * 作用类似,表示0个或一个字符。 |,表示或关系,比如 'gd|good|dog' 表示有gd,good或dog的串 (),将部分内容合成一个单元组。 比如 要搜索 glad 或 good 可以这样 'g(la|oo)d' ()的好处是可以对小组使用 + ? * 等。 比如要搜索A和C开头结尾,中间有至少一个(xyz) 的串,可以这样 : 'A(xyz)+C' 详细出处参考:http://www.jb51.net/article/31207.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值