admin 管理员组

文章数量: 1184232


2023年12月23日发(作者:java9新特性讲解)

grep用法详解:grep与正则表达式

首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!

正则表达式只是一种表示法,只要工具支持这种表示法,那么该工具就可以处理正则表达式的字符串。vi grep ,awk ,sed 等都支持正则表达式.

1基础正则表达式

grep 工具,以前介绍过。

grep -[acinv] '搜索内容串' filename

-a 以文本文件方式搜索

-c 计算找到的符合行的次数

-i 忽略大小写

-n 顺便输出行号

-v 反向选择,即找 没有搜索字符串的行

其中搜索串可以是正则表达式!

1

搜索有the的行,并输出行号

$grep -n 'the' regular_

搜索没有the的行,并输出行号

$grep -nv 'the' regular_

2 利用[]搜索集合字符

[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e

woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_

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_

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_

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_

12:the symbol '*' is represented as star.

搜索以小写字母开头的行

woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_

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_

1:"Open Source" is a good mechanism to develop programs.

21:#I am VBird

woody@xiaoc:~/tmp$

$表示它前面的串是在行的结尾,比如 '.' 表示 . 在一行的结尾

搜索末尾是.的行

woody@xiaoc:~/tmp$ grep -n '.$' regular_ //. 是正则表达式的特殊符号,所以要用转义

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_

22:

23:

woody@xiaoc:~/tmp$

搜索非空行

woody@xiaoc:~/tmp$ grep -vn '^$' regular_

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_

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_ //前两个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,

og...等

woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_

18:google is the best tools for search keyword.

19:goooooogle yes!

搜索g开头和结尾的字符串在的行

woody@xiaoc:~/tmp$ grep -n 'g.*g'

regular_ // .*表示 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_

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_

18:google is the best tools for search keyword.

搜索包含g后面跟2个以上o,后面再跟g的行。。

woody@xiaoc:~/tmp$ grep -n 'go{2,}g' regular_

18:google is the best tools for search keyword.

19:goooooogle yes!

注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。

'[^a-z.!^ -]' 表示没有小写字母,没有. 没有!, 没有空格,没有- 的

串,注意[]里面有个小空格。

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

2扩展正则表达式

扩展正则表达式是对基础正则表达式添加了几个特殊构成的。

它令某些操作更加方便。

比如我们要去除 空白行和行首为 #的行, 会这样用:

woody@xiaoc:~/tmp$ grep -v '^$' regular_ | 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_

"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'

◎grep -- print lines matching a pattern (将符合样式的该行列出)

◎语法: grep [options]

PATTERN []

grep用以在file内文中比对相对应的部分,或是当没有指定档案时,

由标准输入中去比对。 在预设的情况下,grep会将符合样式的那一行列出。

此外,还有两个程式是grep的变化型,egrep及fgrep。

其中egrep就等同於grep -E ,fgrep等同於grep -F 。

◎参数

1. -A NUM,--after-context=NUM

除了列出符合行之外,并且列出後NUM行。

ex: $ grep -A 1 panda file

(从file中搜寻有panda样式的行,并显示该行的後1行)

2. -a或--text

grep原本是搜寻文字档,若拿二进位的档案作为搜寻的目标,

则会显示如下的讯息: Binary file 二进位档名 matches 然後结束。

若加上-a参数则可将二进位档案视为文字档案搜寻,

相当於--binary-files=text这个参数。

ex: (从二进位档案mv中去搜寻panda样式)

(错误)

$ grep panda mv

Binary file mv matches

(这表示此档案有match之处,详见--binary-files=TYPE )

$

(正确)

$ grep -a panda mv

3. -B NUM,--before-context=NUM

与 -A NUM 相对,但这此参数是显示除符合行之外

并显示在它之前的NUM行。

ex: (从file中搜寻有panda样式的行,并显示该行的前1行)

$ grep -B 1 panda file

4. -C [NUM], -NUM, --context[=NUM]

列出符合行之外并列出上下各NUM行,预设值是2。

ex: (列出file中除包含panda样式的行外并列出其上下2行)

(若要改变预设值,直接改变NUM即可)

$ grep -C[NUM] panda file

5. -b, --byte-offset

列出样式之前的内文总共有多少byte ..

ex: $ grep -b panda file

显示结果类似於:

0:panda

66:pandahuang

123:panda03

6. --binary-files=TYPE

此参数TYPE预设为binary(二进位),若以普通方式搜寻,只有2种结果:

1.若有符合的地方:显示Binary file 二进位档名 matches

2.若没有符合的地方:什麽都没有显示。

若TYPE为without-match,遇到此参数,

grep会认为此二进位档案没有包含任何搜寻样式,与-I 参数相同。

若TPYE为text, grep会将此二进位档视为text档案,与-a 参数相同。

Warning: --binary-files=text 若输出为终端机,可能会产生一些不必要的输出。

7. -c, --count

不显示符合样式行,只显示符合的总行数。

若再加上-v,--invert-match,参数显示不符合的总行数。

8. -d ACTION, --directories=ACTION

若输入的档案是一个资料夹,使用ACTION去处理这个资料夹。

预设ACTION是read(读取),也就是说此资料夹会被视为一般的档案;

若ACTION是skip(略过),资料夹会被grep略过:

若ACTION是recurse(递),grep会去读取资料夹下所有的档案,

此相当於-r 参数。

9. -E, --extended-regexp

采用规则表示式去解释样式。

10. -e PATTERN, --regexp=PATTERN

把样式做为一个partern,通常用在避免partern用-开始。

11. -f FILE, --file=FILE

事先将要搜寻的样式写入到一个档案,一行一个样式。

然後采用档案搜寻。

空的档案表示没有要搜寻的样式,因此也就不会有任何符合。

ex: (newfile为搜寻样式档)

$grep -f newfile file

12. -G, --basic-regexp

将样式视为基本的规则表示式解释。(此为预设)

13. -H, --with-filename

在每个符合样式行前加上符合的档案名称,若有路径会显示路径。

ex: (在file与testfile中搜寻panda样式)

$grep -H panda file ./testfile

file:panda

./testfile:panda

$

14. -h, --no-filename

与-H参数相类似,但在输出时不显示路径。

15. --help

产生简短的help讯息。

16. -I

grep会强制认为此二进位档案没有包含任何搜寻样式,

与--binary-files=without-match参数相同。

ex: $ grep -I panda mv

17. -i, --ignore-case

忽略大小写,包含要搜寻的样式及被搜寻的档案。

ex: $ grep -i panda mv

18. -L, --files-without-match

不显示平常一般的输出结果,反而显示出没有符合的档案名称。

19. -l, --files-with-matches

不显示平常一般的输出结果,只显示符合的档案名称。

20. --mmap

如果可能,使用mmap系统呼叫去读取输入,而不是预设的read系统呼叫。

在某些状况,--mmap 能产生较好的效能。 然而,--mmap

如果运作中档案缩短,或I/O 错误发生时,

可能造成未定义的行为(包含core dump),。

21. -n, --line-number

在显示行前,标上行号。

ex: $ grep -n panda file

显示结果相似於下:

行号:符合行的内容

22. -q, --quiet, --silent

不显示任何的一般输出。请参阅-s或--no-messages

23. -r, --recursive

递地,读取每个资料夹下的所有档案,此相当於 -d recsuse 参数。

24. -s, --no-messages

不显示关於不存在或无法读取的错误讯息。

小: 不像GNU grep,传统的grep不符合POSIX.2协定,

因为缺乏-q参数,且他的-s 参数表现像GNU grep的 -q 参数。

Shell Script倾向将传统的grep移植,避开-q及-s参数,

且将输出限制到/dev/null。

POSIX: 定义UNIX及UNIX-like系统需要提供的功能。

25. -V, --version

显示出grep的版本号到标准错误。

当您在回报有关grep的bugs时,grep版本号是必须要包含在内的。

26. -v, --invert-match

显示除搜寻样式行之外的全部。

27. -w, --word-regexp

将搜寻样式视为一个字去搜寻,完全符合该"字"的行才会被列出。

28. -x, --line-regexp

grep参数

1. -c 显示匹配的行数(就是显示有多少行匹配了);

2. -n 显示匹配内容所在文档的行号;

3. -i 匹配时忽略大小写;

4. -s 错误信息不输出;

5. -v 输出不匹配内容;

6. -x 输出完全匹配内容;

7. 忽略表达式中字符原有含义;

8. ^ 匹配表达式的开始行;

9. $ 匹配表达式的结束行;

10. < 从匹配表达式的行开始;

11. > 到匹配表达式的行结束;

12. [ ] 单个字符(如[A] 即A符合要求);

13. [ - ] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求;

14. . 所有的单个字符;

15. * 所有字符,长度可以为0;

[精华] Grep 用法

Grep : g (globally) search for a re (regular expression ) and p (print ) the results.

1、参数:

-I :忽略大小写

-c :打印匹配的行数

-l :从多个文件中查找包含匹配项

-v :查找不包含匹配项的行

-n:打印包含匹配项的行和行标

2、RE(正则表达式)

忽略正则表达式中特殊字符的原有含义

^ 匹配正则表达式的开始行

$ 匹配正则表达式的结束行

< 从匹配正则表达式的行开始

>; 到匹配正则表达式的行结束

[ ] 单个字符;如[A] 即A符合要求

[ - ] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求

. 所有的单个字符

* 所有字符,长度可以为0

3、举例

# ps -ef | grep d

root 19955 181 0 13:43:53 ? 0:00 d

# more size文件的内容

b124230

b034325

a081016

m7187998

m7282064

a022021

a061048

m9324822

b103303

a013386

b044525

m8987131

B081016

M45678

B103303

BADc2345

# more | grep '[a-b]' 范围 ;如[A-Z]即A,B,C一直到Z都符合要求

b124230

b034325

a081016

a022021

a061048

b103303

a013386

b044525

# more

b124230

b034325

a081016

m7187998

m7282064

a022021

a061048

m9324822

b103303

a013386

b044525

m8987131

B081016

M45678

B103303

BADc2345

# more

b124230

b034325

b103303

b044525

# more

b124230

b034325

b103303

b044525

B081016

| grep

| grep

| grep

'[a-b]'*

'' 单个字符;如[A]

'[bB]'

A符合要求 即

B103303

BADc2345

# grep 'root' /etc/group

root::0:root

bin::2:root,bin,daemon

sys::3:root,bin,sys,adm

adm::4:root,adm,daemon

uucp::5:root,uucp

mail::6:root

tty::7:root,tty,adm

lp::8:root,lp,adm

nuucp::9:root,nuucp

daemon::12:root,daemon

# grep '^root' /etc/group 匹配正则表达式的开始行

root::0:root

# grep 'uucp' /etc/group

uucp::5:root,uucp

nuucp::9:root,nuucp

# grep '

uucp::5:root,uucp

# grep 'root$' /etc/group 匹配正则表达式的结束行

root::0:root

mail::6:root

# more | grep -i 'b1..*3' -i :忽略大小写

b124230

b103303

B103303

# more | grep -iv 'b1..*3' -v :查找不包含匹配项的行

b034325

a081016

m7187998

m7282064

a022021

a061048

m9324822

a013386

b044525

m8987131

B081016

M45678

BADc2345

# more | grep -in 'b1..*3'

1:b124230

9:b103303

15:B103303

# grep '$' /etc/init.d/ | wc -l

128

# grep '$' /etc/init.d/ | wc –l 忽略正则表达式中特殊字符的原有含义

15

# grep '$' /etc/init.d/

case "$1" in

>;/tmp/sharetab.$$

[ "x$fstype" != xnfs ] &&

echo "$patht$rest$fstypet$optst$desc"

>;>;/tmp/sharetab.$$

/usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab.$$

/usr/bin/mv -f /tmp/sharetab.$$ /etc/dfs/sharetab

if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[ ]*(#|$)'

if [ $startnfsd -eq 0 -a -f /etc/ ] &&

if [ $startnfsd -ne 0 ]; then

elif [ ! -n "$_INIT_RUN_LEVEL" ]; then

while [ $wtime -gt 0 ]; do

wtime=`expr $wtime - 1`

if [ $wtime -eq 0 ]; then

echo "Usage: $0 { start | stop }"

# more

the test file

their are files

The end

# grep 'the'

the test file

their are files

# grep '

the test file

their are files

# grep 'the>;'

the test file

# grep ';'

the test file

# grep '<[Tt]he>;'

the test file

The end


本文标签: 档案 符合 显示 没有 字符