Chinaunix首页 | 论坛 | 博客
  • 博客访问: 252739
  • 博文数量: 58
  • 博客积分: 2241
  • 博客等级: 大尉
  • 技术积分: 522
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-07 09:53
文章分类
文章存档

2012年(4)

2011年(19)

2010年(31)

2009年(4)

分类: LINUX

2011-08-08 14:51:55

1. grep简介

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

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

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

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

^

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

$

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

.

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

*

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

[]

匹配一个指定范围内的字符,如'[Gg]rep'匹配Grepgrep

[^]

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

/(../)

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

/<

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

/>

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

x/{m/}

重复字符xm次,如:'0/{5/}'匹配包含5o的行。

x/{m,/}

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

x/{m,n/}

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

/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

匹配abc。如:grep|sed匹配grepsed

()

分组符号,如:love(able|rs)ov+匹配loveablelovers,匹配一个或多个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下的grepfgrep外,都支持POSIX的字符类。

[:alnum:]

文字数字字符

[:alpha:]

文字字符

[:digit:]

数字字符

[:graph:]

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

[:lower:]

小写字符

[:cntrl:]

控制字符

[:print:]

非空字符(包括空格)

[:punct:]

标点符号

[:space:]

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

[:upper:]

大写字符

[:xdigit:]

十六进制数字(0-9a-fA-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

显示在aabbcc文件中匹配test的行。

$ grep '[a-z]/{5/}' aa

显示所有包含每个字符串至少有5个连续小写字符的字符串的行。

$ grep 'w/(es/)t.*/1' aa

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


grep 参数使用和实例 2008-07-20 10:25:33
原始出处http://future.blog.51cto.com/26959/88653

一、grep 参数使用
Gun grep   选项
-b   在搜索到的行的前面打印该行所在的块号码。
-c   只显示有多少行匹配 ,而不具体显示匹配的行
-h   不显示文件名
-i    在字符串比较的时候忽略大小写
-l    只显示包含匹配模板的行的文件名清单,不同项目之间用换行符分隔
-L   打印不匹配模板的文件名清单
-n   在每一行前面打印该行在文件中的行数
-s   静默工作,除非出现错误信息否则不打印任何信息,这个功能在检测退出状态的时候有用
-v   反检索,只显示不匹配的行
-w 
-Ax  在匹配指定行打印完毕后,再打印x行(向原文件匹配行下x行)
-By  在匹配指定行前面打印y行(在原文件匹配行上面打印y行)
-Cz  在匹配行前后打印z行  (在原文件匹配行上下打印z行)
-b    在每一行前面打印字符偏移量
-f  file   从文件file中提取模板。空文件中包含0个模板
-q     取消标准输出,跟-n功能是一样的
-s     不显示关于不存在或者无法读文件的错误信息
-w   只打印以单词形式匹配模板的行,模板可以是包含数字、字符和下划线的字符串
-x    只打印整行匹配的行
-y   用法同-i
-U  把文件作为二进制文件,这个选项只在MS-DOS和MS-Windows中被支持(这个参数没有明白,请过路高人指点,非常感谢)
-u 按照unix风格报告字符偏移量。只在-b选项同时被使用的时候才有效。这个选项只在MS-DOS和MS-Windows中被支持
 
grep "$name" file  把变量$name 的值作为模板,在文件中寻找匹配模板的行。注意,必须使用双引号
 
重复作用的元字符, \{\}; 用来做标签的元字符,\(\); 用来锚定单词的元字符\<\>
 
二、实例
数据文件
[root@future tmp]# cat newbo
1  aa
2  AA
3  Aa
4
5
[root@future tmp]# cat bo
1
2
sss   bo-
3
4
5.8 shell  5      5
6
hello grep 5
 
[root@future root]# grep -n "" bo
1:1
2:2
3:sss   bo-
4:3
5:4
6:5.8 shell  5      5
7:6
8:hello grep 5
 
[root@future tmp]# grep -i "a" newbo       -i关闭大小写敏感
1  aa
2  AA
3  Aa
 
[root@future tmp]# grep -l '2' *                -l 打印匹配的文件名,而不打印匹配的行
bo
newbo
 
[root@future tmp]# grep -c 'a' newbo     打印有多少匹配行
2
[root@future tmp]# grep -c -i 'a' newbo
3
 
[root@future tmp]# grep -w 'shell' bo   -w打印按照单词方式匹配模板的行,而不是作为单词的一部分匹配模板的行
5.8 shell  5      5
[root@future tmp]# grep -w 'hell' bo
[root@future tmp]#
 
[root@future tmp]# grep -2 '3' newbo      上下各两行
1  aa
2  AA
3  Aa
4
5
[root@future tmp]# grep -A2 '3' newbo  下两行
3  Aa
4
5
[root@future tmp]# grep -B2 '3' newbo   上两行
1  aa
2  AA
3  Aa
[root@future tmp]# grep -C2 '3' newbo    上下各两行
1  aa
2  AA
3  Aa
4
5
[root@future tmp]#
 
[root@future tmp]# grep -b '' newbo   打印每一行前打印字符偏移量(不明白具体指的是什么感觉比较抽象,在次请高人指教)
0:1  aa
6:2  AA
12:3  Aa
18:4
20:5
 
[root@future tmp]# cat regular
1
2
[root@future tmp]# grep -f regular newbo   -f 是指从文件中读取模板 
1  aa
2  AA
[root@future tmp]#
 
[root@future tmp]# grep '' *
bo:1
bo:2
bo:sss   bo-
bo:3
bo:4
bo:5.8 shell  5      5
bo:6
bo:hello grep 5
newbo:1  aa
newbo:2  AA
newbo:3  Aa
newbo:4
newbo:5
regular:1
regular:2
[root@future tmp]# grep -h '' *    -h 使得grep不打印头信息,这个例子中是不打印文件名
1
2
sss   bo-
3
4
5.8 shell  5      5
6
hello grep 5
1  aa
2  AA
3  Aa
4
5
1
2
 
[root@future tmp]# grep -q '' " newbo   -q取消grep的所有输出,在只需要退出状态值的场合这个选项就显得非常有用
[root@future tmp]# echo $?
0
[root@future tmp]#
 
 
 
 
 
 
数据文件
[root@future root]# cat bo
1
2
sss   bo-
3
4
5.8 shell  5      5
6
hello grep 5
[root@future root]# cat nu
1
ss   -bo
2
xx    bo-
2222222222
3
4
 
[root@future root]# grep 2 * 在所有的文件中搜索“2”
bo:2
nu:2
nu:2222222222
 
[root@future root]# grep '^5' *        ^锚定行的开始
bo:5.8 shell  5      5
 
[root@future root]# grep '5$' bo      $锚定行的结尾
5.8 shell  5      5
hello grep 5
 
[root@future root]# grep '5\..' bo     第一个字符是5,然后紧跟着一个. 尔后是一个任意字符
5.8 shell  5      5
如果点前面有一个反斜杠,则点就不再特殊,而是仅仅表示一个点。
 
[root@future root]# grep "^[23]" bo  表示开头是以2或3开头的行
2
3
 
[root@future root]# grep [^0-9] bo       打印所有包含非数字字符的行
sss   bo-
5.8 shell  5      5
hello grep 5
[root@future root]# grep '[^0-9]' bo
sss   bo-
5.8 shell  5      5
hello grep 5
[root@future root]# grep "[^0-9]" bo
sss   bo-
5.8 shell  5      5
hello grep 5
 
[root@future root]# grep "^[^0-9]" bo    打印所有不是以数字开头的行
sss   bo-
hello grep 5
 
[root@future root]# grep '[a-z]\{5\}' bo   打印每个字符串至少有5个连续小写字母的字符串的行
5.8 shell  5      5
hello grep 5
[root@future root]# grep '^[a-z]\{5\}' bo打印开头字符串至少有5个连续小写字母的字符串的行
hello grep 5
 
[root@future root]# grep '\(5\)\.[0-9].*\1 *\1' bo  打印第一个字符是5,紧跟着一个句点,然后是任意一个数字,然后是任意字符,然后是一个5,然后是一个任意个制表符,然后又是一个5。
5.8 shell  5      5
 
[root@future root]# grep '\(5\)\.[0-9] \1 *\1' bo 之所以没有搜索到是因为没有“.*”任意字符
[root@future root]# grep '\(5\)\.[0-9] \1 .*\1' bo
[root@future root]# grep '\(5\)\.[0-9] .* \1 *\1' bo
5.8 shell  5      5
\1 是一个引用,含义是正则表达式中第一个被\(和\)括起来的部分
 
[root@future root]# grep '\hello grep 5
 
[root@future root]# grep '5\>' bo         锚定单词的结尾
5.8 shell  5      5
hello grep 5
 
[root@future root]# grep '\' bo
[root@future root]# grep '\' bo    打印所有包含单词hello的行
hello grep 5
 
[root@future root]# grep 'hello' bo
hello grep 5
[root@future root]# grep '\hello\b' bo   \b是单词分界符  没有理解这个参数请路过的高人注解
hello grep 5
 
[root@future root]# grep '^h\w*\w' bo   也没有理解\w参数,仍然请过路高人注解,万分感激!
hello grep 5
 
[root@future root]# grep '\<[a-z].*o\>' bo              .* 代表任意字符
sss   bo-
hello grep 5
 
[root@future root]# grep '\<[a-z]\{4\}o\>' bo        重复4次,加上前面一次,共5次
hello grep 5

[root@future root]# grep '\<[a-z]\{5\}o\>' bo
[root@future root]# grep '\<[a-z]\{3,20\}o\>' bo
hello grep 5
[root@future root]# grep '\<[a-z]\{6,20\}o\>' bo
[root@future root]# grep '\<[a-z]\{5,20\}o\>' bo
[root@future root]#
[root@future root]# grep '\<[a-z]\{4,20\}o\>' bo  重复4次
hello grep 5
 
[root@future root]# grep '\<[a-z]\{3,5\}o\>' bo  
hello grep 5
[root@future root]# grep '\<[a-z]\{5,6\}o\>' bo
[root@future root]#
 
注:a\{x,y\}\  意义是重复a+(x和y之间含x和y)
 
 
[root@future root]# grep '2|3' bo        grep不支持扩展的正则表达式,竖线是用于表示‘或’的扩展正则表达式元字符。所以没有输出。
[root@future root]# grep '2\|3' bo      加上反斜杠,这个字符就被翻译成扩展正则表达式。
2
3
[root@future root]# grep "\(2\|3\)" bo
2
3
 
 
[root@future root]# grep "\(5\)\.8.*\1" bo
5.8 shell  5      5
正则表达式5.8被匹配,模板5 就被存储到内存中的寄存器1内,这个正则表达式的含义是如果5.8被找到,标记并保存5,然后搜索任意个字符(.*),尔后是一个\1代表5
[root@future root]# grep "\(5\).*\1" bo
5.8 shell  5      5
 
 
[root@test-linux tmp]# cat te
adlkf adfkl aa        566.5
dfad 234.43 aaa
234           aaaa
adf adfa adf adf 45.556  aaaaa
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]# grep '\baaa\b' te      \b 单词分界符
dfad 234.43 aaa
[root@test-linux tmp]#
 
 
[root@test-linux tmp]# cat te
adlkf adfkl aa        566.5
dfad. 234.43 aaa
dfad . 234.43 aaa
234           aaaa
adf adfa adf adf 45.556  aaaaa
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]# grep '^d\w' te   \w 字母数字字符[a-zA-Z0-9]  \W 非字母数字字符
dfad. 234.43 aaa
dfad . 234.43 aaa
[root@test-linux tmp]#


linux下grep命令用法实例教程

一,grep命令有什么用

个人觉得grep命令就是一个对文本或输出进行匹配并控制输出的一个工具,看一下下面的参数,部分翻译了,有不对的地方,还请指正

  1. grep --help  
  2. 匹配模式选择:  
  3.  -E, --extended-regexp     扩展正则表达式egrep  
  4.  -F, --fixed-strings       一个换行符分隔的字符串的集合fgrep  
  5.  -G, --basic-regexp        基本正则  
  6.  -P, --perl-regexp         调用的perl正则  
  7.  -e, --regexp=PATTERN      后面根正则模式,默认无  
  8.  -f, --file=FILE           从文件中获得匹配模式  
  9.  -i, --ignore-case         不区分大小写  
  10.  -w, --word-regexp         匹配整个单词  
  11.  -x, --line-regexp         匹配整行  
  12.  -z, --null-data           a data line ends in 0 byte, not newline  
  13.   
  14. 杂项:  
  15.  -s, --no-messages         不显示错误信息  
  16.  -v, --invert-match        显示不匹配的行  
  17.  -V, --version             显示版本号  
  18.  --help                    显示帮助信息  
  19.  --mmap                use memory-mapped input if possible  
  20.   
  21. 输入控制:  
  22.  -m, --max-count=NUM       匹配的最大数  
  23.  -b, --byte-offset         打印匹配行前面打印该行所在的块号码。  
  24.  -n, --line-number         显示的加上匹配所在的行号  
  25.  --line-buffered           刷新输出每一行  
  26.  -H, --with-filename       当搜索多个文件时,显示匹配文件名前缀  
  27.  -h, --no-filename         当搜索多个文件时,不显示匹配文件名前缀  
  28.  --label=LABEL            print LABEL as filename for standard input  
  29.  -o, --only-matching       show only the part of a line matching PATTERN  
  30.  -q, --quiet, --silent     不显示任何东西  
  31.  --binary-files=TYPE   assume that binary files are TYPE  
  32.  TYPE is 'binary''text'or 'without-match'  
  33.  -a, --text                匹配二进制的东西  
  34.  -I                        不匹配二进制的东西  
  35.  -d, --directories=ACTION  目录操作,读取,递归,跳过  
  36.  ACTION is 'read''recurse'or 'skip'  
  37.  -D, --devices=ACTION      设置对设备,FIFO,管道的操作,读取,跳过  
  38.  ACTION is 'read' or 'skip'  
  39.  -R, -r, --recursive       递归调用  
  40.  --include=PATTERN     files that match PATTERN will be examined  
  41.  --exclude=PATTERN     files that match PATTERN will be skipped.  
  42.  --exclude-from=FILE   files that match PATTERN in FILE will be skipped.  
  43.  -L, --files-without-match 匹配多个文件时,显示不匹配的文件名  
  44.  -l, --files-with-matches  匹配多个文件时,显示匹配的文件名  
  45.  -c, --count               显示匹配了多少次  
  46.  -Z, --null                print 0 byte after FILE name  
  47.   
  48. 文件控制:  
  49.  -B, --before-context=NUM  打印匹配本身以及前面的几个行由NUM控制  
  50.  -A, --after-context=NUM   打印匹配本身以及随后的几个行由NUM控制  
  51.  -C, --context=NUM         打印匹配本身以及随后,前面的几个行由NUM控制  
  52.  -NUM                      根-C的用法一样的  
  53.  --color[=WHEN],  
  54.  --colour[=WHEN]       use markers to distinguish the matching string  
  55.  WHEN may be `always', `never' or `auto'.  
  56.  -U, --binary              do not strip CR characters at EOL (MSDOS)  
  57.  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)  

二,准备测试文件test

  1. root:x:0:0:root:/root:/bin/bash  
  2. bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa  
  3. DADddd:x:2:2:daemon:/sbin:/bin/false  
  4. mail:x:8:12:mail:/var/spool/mail:/bin/false  
  5. ftp:x:14:11:ftp:/home/ftp:/bin/false  
  6. &nobody:$:99:99:nobody:/:/bin/false  
  7. zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  
  8. http:x:33:33::/srv/http:/bin/false  
  9. dbus:x:81:81:System message bus:/:/bin/false  
  10. hal:x:82:82:HAL daemon:/:/bin/false  
  11. mysql:x:89:89::/var/lib/mysql:/bin/false  
  12. aaa:x:1001:1001::/home/aaa:/bin/bash  
  13. ba:x:1002:1002::/home/zhangy:/bin/bash  
  14. test:x:1003:1003::/home/test:/bin/bash  
  15. @zhangying:*:1004:1004::/home/test:/bin/bash  
  16. policykit:x:102:1005:Po  

这个测试文件,根介绍sed和awk命令时用的一样的,是个密码文件。

三,应用举例

  1. [root@krlcgcms01 test]# grep root test  
  2. root:x:0:0:root:/root:/bin/bash  

匹配含有root的行

  1. [root@krlcgcms01 test]# cat test |grep '^\(root\|zhang\)'  
  2. root:x:0:0:root:/root:/bin/bash  
  3. zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

匹配以root开头或者以zhang开头的行,注意反斜杠

  1. [root@krlcgcms01 test]# cat test |grep -e '^\(root\|zhang\)'  
  2. root:x:0:0:root:/root:/bin/bash  
  3. zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

匹配以root开头或者以zhang开头的行,注意反斜杠,根上面一个例子一样,-e默认是省去的

  1. [root@krlcgcms01 test]# echo 'zhangying' |grep '^zhang[a-z]*$'  
  2. zhangying  

匹配以zhang开头,只含有字母

  1. [root@krlcgcms01 test]# cat test |grep -E '^bin'  
  2. bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa  

匹配以bin开头的行,用的egrep,在这里可以换成-F,-G

  1. [root@krlcgcms01 test]# cat test|grep -n zhangy  
  2. 7:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  
  3. 13:ba:x:1002:1002::/home/zhangy:/bin/bash  
  4. 15:@zhangying:*:1004:1004::/home/test:/bin/bash  

在匹配的行前面加上该行在文件中,或者输出中所在的行号

  1. [root@krlcgcms01 test]# cat test|grep -nv bin  
  2. 16:policykit:x:102:1005:Po  

不匹配以bin开头的行,并显示行号

  1. [root@krlcgcms01 test]#  cat test|grep -c zhang  
  2. 3  

显示匹配的个数,不显示内容

  1. [root@krlcgcms01 test]# grep  system test  
  2. [root@krlcgcms01 test]# grep -ni  system test  
  3. 9:dbus:x:81:81:System message bus:/:/bin/false  

匹配system,没有加-i没有匹配到东西。

  1. [root@krlcgcms01 test]#  cat test|grep -w zhan  
  2. [root@krlcgcms01 test]#  cat test|grep -w zhangy  
  3. zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  
  4. ba:x:1002:1002::/home/zhangy:/bin/bash  

匹配zhan没有匹配到东西,匹配zhangy能匹配到,因为在test文件中,有zhangy这个单词

  1. [root@krlcgcms01 test]# echo "aaaaaa" |grep -x aaa  
  2. [root@krlcgcms01 test]# echo "aaaa" |grep -x aaaa  
  3. aaaa  

在这里-x后面东西,和输出中的整行相同时,才会输出

  1. [root@krlcgcms01 test]# cat test |grep -m 1 zhang  
  2. zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

最多只匹配一次,如果把-m 1去掉的话,会有三个

  1. [apacheuser@krlcgcms01 test]$ cat test |grep -b zha  
  2. 241:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  
  3. 480:ba:x:1002:1002::/home/zhangy:/bin/bash  
  4. 558:@zhangying:*:1004:1004::/home/test:/bin/bash  

匹配行的前面显示块号,这个块号是干什么的,不知道,有谁知道可否告诉我一下

  1. [apacheuser@krlcgcms01 test]$ grep -H 'root' test test2 testbak  
  2. test:root:x:0:0:root:/root:/bin/bash  
  3. test2:root  
  4. testbak:root:x:0:0:root:/root:/bin/bash  

多文件匹配时,在匹配的行前面加上文件名

  1. [apacheuser@krlcgcms01 test]$ grep -h 'root' test test2 testbak  
  2. root:x:0:0:root:/root:/bin/bash  
  3. root  
  4. root:x:0:0:root:/root:/bin/bash  

多文件匹配时,在匹配的行前面不加上文件名

  1. [apacheuser@krlcgcms01 test]$ grep -l 'root' test test2 testbak DAta  
  2. test  
  3. test2  
  4. testbak  

多文件匹配时,显示匹配文件的文件名

  1. [apacheuser@krlcgcms01 test]$ grep -L 'root' test test2 testbak DAta  
  2. DAta  

多文件匹配时,在匹配的行前面不加上文件名

  1. [apacheuser@krlcgcms01 test]$ grep  'root' test  
  2. root:x:0:0:root:/root:/bin/bash  
  3. [apacheuser@krlcgcms01 test]$ grep -o 'root' test  
  4. root  
  5. root  
  6. root  

没有-o时,有一行匹配,这一行里面有3个root,加上-o后,这个3个root就出来了

  1. [apacheuser@krlcgcms01 test]$ grep -V  
  2. grep (GNU grep) 2.5.1  
  3.   
  4. Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.  

显示版本

  1. [apacheuser@krlcgcms01 test]$ grep -q 'root' test  

不显示任何内容

  1. [root@krlcgcms01 test]# grep test -R /tmp/test/mytest  
  2. /tmp/test/mytest/test:test:x:1003:1003::/home/test:/bin/bash  
  3. /tmp/test/mytest/test:@zhangying:*:1004:1004::/home/test:/bin/bash  

递归显示匹配的内容,在test目录下面建个mytest目录,copy test目录下面的test文件到mytest下面,能看到上面的结果

  1. [root@krlcgcms01 test]# cat test |grep -A 3 root  
  2. root:x:0:0:root:/root:/bin/bash  
  3. bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa  
  4. daemon:x:2:2:daemon:/sbin:/bin/false  
  5. mail:x:8:12:mail:/var/spool/mail:/bin/false  

显示匹配root后面的3行

  1. [root@krlcgcms01 test]# cat test |grep -B 2 ftp  
  2. daemon:x:2:2:daemon:/sbin:/bin/false  
  3. mail:x:8:12:mail:/var/spool/mail:/bin/false  
  4. ftp:x:14:11:ftp:/home/ftp:/bin/false  

显示匹配ftp前面的2行

  1. [root@krlcgcms01 test]# cat test |grep -C 2 ftp  
  2. daemon:x:2:2:daemon:/sbin:/bin/false  
  3. mail:x:8:12:mail:/var/spool/mail:/bin/false  
  4. ftp:x:14:11:ftp:/home/ftp:/bin/false  
  5. &nobody:$:99:99:nobody:/:/bin/false  
  6. zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

显示匹配ftp前面的2行,后面的2行,以及本身

  1. [root@krlcgcms01 test]# cat test |grep -2 ftp  
  2. daemon:x:2:2:daemon:/sbin:/bin/false  
  3. mail:x:8:12:mail:/var/spool/mail:/bin/false  
  4. ftp:x:14:11:ftp:/home/ftp:/bin/false  
  5. &nobody:$:99:99:nobody:/:/bin/false  
  6. zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

显示匹配ftp前面的2行,后面的2行,以及本身,和-C用法一样


阅读(1069) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~