linux下grep命令用法实例教程

张映 发表于 2010-09-10

分类目录: linux

标签:, , , , ,

一,grep命令有什么用

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

grep --help
匹配模式选择:
 -E, --extended-regexp     扩展正则表达式egrep
 -F, --fixed-strings       一个换行符分隔的字符串的集合fgrep
 -G, --basic-regexp        基本正则
 -P, --perl-regexp         调用的perl正则
 -e, --regexp=PATTERN      后面根正则模式,默认无
 -f, --file=FILE           从文件中获得匹配模式
 -i, --ignore-case         不区分大小写
 -w, --word-regexp         匹配整个单词
 -x, --line-regexp         匹配整行
 -z, --null-data           a data line ends in 0 byte, not newline

杂项:
 -s, --no-messages         不显示错误信息
 -v, --invert-match        显示不匹配的行
 -V, --version             显示版本号
 --help                    显示帮助信息
 --mmap                use memory-mapped input if possible

输入控制:
 -m, --max-count=NUM       匹配的最大数
 -b, --byte-offset         打印匹配行前面打印该行所在的块号码。
 -n, --line-number         显示的加上匹配所在的行号
 --line-buffered           刷新输出每一行
 -H, --with-filename       当搜索多个文件时,显示匹配文件名前缀
 -h, --no-filename         当搜索多个文件时,不显示匹配文件名前缀
 --label=LABEL            print LABEL as filename for standard input
 -o, --only-matching       show only the part of a line matching PATTERN
 -q, --quiet, --silent     不显示任何东西
 --binary-files=TYPE   assume that binary files are TYPE
 TYPE is 'binary', 'text', or 'without-match'
 -a, --text                匹配二进制的东西
 -I                        不匹配二进制的东西
 -d, --directories=ACTION  目录操作,读取,递归,跳过
 ACTION is 'read', 'recurse', or 'skip'
 -D, --devices=ACTION      设置对设备,FIFO,管道的操作,读取,跳过
 ACTION is 'read' or 'skip'
 -R, -r, --recursive       递归调用
 --include=PATTERN     files that match PATTERN will be examined
 --exclude=PATTERN     files that match PATTERN will be skipped.
 --exclude-from=FILE   files that match PATTERN in FILE will be skipped.
 -L, --files-without-match 匹配多个文件时,显示不匹配的文件名
 -l, --files-with-matches  匹配多个文件时,显示匹配的文件名
 -c, --count               显示匹配了多少次
 -Z, --null                print 0 byte after FILE name

文件控制:
 -B, --before-context=NUM  打印匹配本身以及前面的几个行由NUM控制
 -A, --after-context=NUM   打印匹配本身以及随后的几个行由NUM控制
 -C, --context=NUM         打印匹配本身以及随后,前面的几个行由NUM控制
 -NUM                      根-C的用法一样的
 --color[=WHEN],
 --colour[=WHEN]       use markers to distinguish the matching string
 WHEN may be `always', `never' or `auto'.
 -U, --binary              do not strip CR characters at EOL (MSDOS)
 -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

二,准备测试文件test

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

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

三,应用举例

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

匹配含有root的行

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[apacheuser@krlcgcms01 test]$ grep -l 'root' test test2 testbak DAta
test
test2
testbak

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

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

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

[apacheuser@krlcgcms01 test]$ grep  'root' test
root:x:0:0:root:/root:/bin/bash
[apacheuser@krlcgcms01 test]$ grep -o 'root' test
root
root
root

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

[apacheuser@krlcgcms01 test]$ grep -V
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.

显示版本

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

不显示任何内容

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

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

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

显示匹配root后面的3行

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

显示匹配ftp前面的2行

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

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

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

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

grep命令的参数,可以排组合的,根据个人的需要。例子还是不全,欢迎补充



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/linux/1008.html

1 条评论

  1. Jordan fans 留言