曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5081|回复: 3
打印 上一主题 下一主题

grep

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
跳转到指定楼层
楼主
发表于 2009-1-7 23:24:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
grep 的三种变形

grep : 标准 grep 命令

egrep : 扩展 gerp, 支持基本及扩展的正则表达式

fgrep : 快速 grep

grep 一般格式为

grep [ 选项 ] 基本正则表达式 [ 文件 ]

注意,字符串参数最好才用双引号括起来,一是以防被误解为 shell 命令,二是可以用来查找多个单词组成的字符串

常用命令选项

-c   只输出匹配行的计数
-i    不区分大小写
-h   查询多文件时不显示文件名
-H  显示文件名
-l    查询多文件时只输出包含匹配字符的文件名
-n   显示匹配行及行号
-s   不显示不存在不存在或无匹配文本的错误信息
-v   显示不包含匹配文本的所有行
-o   仅匹配
-b   总是与 -o 选项一起使用
-e   同时指定多个匹配
-f   指定包含有匹配项的文件
-R/-r  递归查找目录下的所有文件
--include  指定包含的文件(使用通配符匹配)
--exclude 排除掉指定文件
-A NUM 打印匹配行之后的 NUM 行
-B NUM 打印匹配行之前的 NUM 行
-C NUM 既打印匹配行之前的 NUM 行也打印之后的 NUM 行




先假设目录下有两个文件,文件名和内容分别为:

grep-1.txt:
beyes
root
admin
qinhe
duoduo
qingqing
xiaohe
duoduo
--------------------------------------
grep-2.txt
sort it
2009
2009
2008
Jul
jul
jUl
juL
2009:22
2009:22:51
2100
01200

找出含有 duoduo 字符串的文件
[beyes@localhost basic_test]$ grep "duoduo" *.txt
grep-1.txt:duoduo
grep-1.txt:duoduo


在所有文件中查找含有 sort it 的文件
[beyes@localhost basic_test]$ grep "sort it" *
grep-2.txt:sort it


使用 -c  参数
[beyes@localhost basic_test]$ grep -c "duoduo" grep-1.txt
2


使用 -n 参数
[beyes@localhost basic_test]$ grep -n "duoduo" grep-1.txt
5:duoduo
8:duoduo


使用 -i 参数
[beyes@localhost basic_test]$ grep -i "jul" grep-2.txt
Jul
jul
jUl
juL


使用 -v 参数(过滤):
[beyes@localhost basic_test]$ grep -v "2009:22" grep-2.txt     #过滤掉含有 2009 的一行后并打印文本
sort it
2008
Jul
jul
jUl
juL
2100
01200


简单正则表达式查找到 2009:22:51 的行
[beyes@localhost basic_test]$ grep "2009:22:5[0-9]" grep-2.txt
2009:22:51




几个使用 ^ 和 [ ] 正则表达式的比较

1、列出所有包含有 2 字符的行
[beyes@localhost basic_test]$ grep "[2]" grep-2.txt
2009
2009
2008
2009:22
2009:22:51
2100
01200
2、列出仅以 2 字符开头的行
[beyes@localhost basic_test]$ grep "^2" grep-2.txt
2009
2009
2008
2009:22
2009:22:51
2100

3、列出不含有 2 开头的行
[beyes@localhost basic_test]$ grep "^[^2]" grep-2.txt          #再取一次 ^ 则为过滤(补集),^^2 -- 这样的形式无效
sort it
Jul
jul
jUl
juL
01200

-o 选项表示仅匹配
# echo GNU is not UNIX |grep -o "UNIX"
UNIX

-b 选项仅和 -o 选项一起使用,表示字节偏移
root@bt:~# echo "GNU is not UNIX" |grep -b -o "UNIX"
11:UNIX
位置偏移是从 0 开始算起的。

-e 选项指定多个匹配
root@bt:~# echo hello www groad net |grep -e "groad" -e "net" -o
groad
net

-f 选项指定包含多个匹配项的文件
像上面,用 -e 可以指定多个匹配项,但是方法不止这个,如果有更多的匹配项,我们可能不想这么多的都写在命令行里,因此 grep 也提供了 -f 选项,它允许我们将多个匹配项写到一个文件中,而用 -f 指定出该文件,比如:
root@bt:~# cat pfile.txt
groad
net
root@bt:~# echo hello www groad net |grep -f pfile.txt -o
groad
net

-R 选项可以让我们递归的处理某目录下的所有文件
root@bt:~/src# grep "hello_test()" . -R -n
./misc/hello.c:3:int hello_test()
./misc/hello.c:11:      hello_test();
该选项对于查找开发者查找代码中的相关函数是非常有用的。

在 --include 选项指定的文件中处理
root@bt:~/src# grep "hello_test()" . -r --include *.{c,cpp}
./misc/hello.c:int hello_test()
./misc/hello.c:    hello_test();
上面的 misc 目录下还有一个 hello.txt 的文件,里面也有 "hello_test()" 这一句,但我们在命令行中说明了只在 --include 所指定的文件类型(.c 和 .cpp)中查找。

在 --exclude 排除之外的文件中进行处理
和 --include 相对应的,--exclude 排除要在某些文件中查找,比如:
root@bt:~/src# grep "hello_test()" . -r --exclude *.c
./misc/hello.txt:hello_test()

-A NUM 打印匹配行之后的 NUM 行
# seq 10 | grep 5 -A 3
5
6
7
8
另外,如果有多行匹配,那么匹配块将以 -- 符号进行分组:
root@bt:~/src# echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b
root@bt:~/src# echo -e "a\nb\nc\nd\na\nb" | grep a -A 1
a
b
--
a
b
root@bt:~/src# echo -e "a\nb\nc\nd\nc\na" | grep a -A 1
a
b
--
a

-B NUM 打印匹配行之前的 NUM 行
# seq 10 | grep 5 -B 3
2
3
4
5

-C NUM 既打印匹配行之前的 NUM 行也打印之后的 NUM 行
# seq 10 | grep 5 -C 3
2
3
4
5
6
7
8

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
沙发
 楼主| 发表于 2009-1-8 20:33:37 | 只看该作者

grep 命令类名

等价的正则表达式
[[:upper:]]
[A-Z]
[[:alnum:]]
[0-9a-zA-Z]
[[:lower:]]
[a-z]
[[:space:]]
空格或tab键
[[:digit:]]
[0-9]
[[:alpha:]]
[a-zA-Z]


例如:

grep "5[[:digit:]][[:digit:]]" test.txt

相当于:

grep "5[0-9][0-9]" test.txt

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
板凳
 楼主| 发表于 2009-1-8 19:50:00 | 只看该作者

正则表达式举例

有文件 grep-3.txt ,其中内容为
groad.net
beyes@groad.net
g-d-m
NO:561
NO:873
445511
455114
444511
411111
404040404
444441111
111114444
??hello
how are you??
[quote]

列出含有以 g 开头且以 d 结尾字符串的行
[quote]
[beyes@localhost basic_test]$ grep "g*d" grep-3.txt
groad.net
beyes@groad.net
g-d-m


列出含有第一个数字在 5-8之 间,第二个数字为 6-9 之间,第三个数字为 0-3 之间的数字的行
[beyes@localhost basic_test]$ grep "[5-8][6-9][0-3]" grep-3.txt
NO:561
NO:873


列出含有连续两个 4 的行
[beyes@localhost basic_test]$ grep "4\\{2\\}" grep-3.txt
445511
444511
444441111
111114444


列出至少含有两个连续的 4 的行
[beyes@localhost basic_test]$ grep "4\\{2,\\}" grep-3.txt
445511
444511
444441111
111114444


列出含有两个到四个连续的 4  的行
[beyes@localhost basic_test]$ grep "4\\{2,4\\}" grep-3.txt
445511
444511
444441111
111114444


找出空行,并打印出空行所在的行号
[beyes@localhost basic_test]$ grep "^$" -n grep-3.txt
7:
9:
11:

找出含有 ?  号的行
[beyes@localhost basic_test]$ grep "\\?" grep-3.txt
??hello
how are you??

找出以字母 h 开头的行
[beyes@localhost basic_test]$ grep "^h" grep-3.txt
how are you??

找出不含有以字母 g  开头的行
[beyes@localhost basic_test]$ grep "^[^g]" grep-3.txt
beyes@groad.net
NO:561
NO:873
445511
455114
444511
411111
404040404
444441111
111114444
??hello
how are you??

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
地板
 楼主| 发表于 2012-4-3 23:31:25 | 只看该作者

grep 和 xargs 的配合使用

xargs 可以为另外一个命令提供参数列表;当文件名作为指定命令的命令行参数时,通常建议使用 '\\0' (zero-byte) 结尾的文件名,而不是空格作为结尾(默认如此,但是在一些文件名是含有空格的,此时会造成找不到相应文件的处理错误)。xargs 通常搭配 find, grep 这些命令使用,它从管道接收从来自这些命令的输出,如果想处理 '\\0' 结尾的文件名,那么 xargs 需要指定 -0 选项。

下面举例:

先创建几个文件
# touch "www groad.txt"
# vi groad\\ net.txt
# cat groad\\ net.txt
groad
# echo groad > groad.txt
# echo "good website" > www.txt

尝试运行下面的命令:
# grep "groad" *.txt -lZ |xargs rm
xargs: Warning: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
rm: cannot remove `groad': No such file or directory
rm: cannot remove `net.txt': No such file or directory
上面,-l 选项表示输出匹配的文件名, -Z 选项表示这些文件名以 '\\0' 结尾,这两个选项通常是结合一起使用的。
由输出可见,发生了解析错误,它认为 groad 和 net.txt 是两个不同的文件(实际上它们是同一个)。

因此下面运行的命令才是正确的:
root@bt:~/src# grep "groad" *.txt -lZ |xargs -0 rm
root@bt:~/src# ls
misc  www.txt
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-6-18 07:30 , Processed in 0.080280 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表