grep入门学习笔记

来源:互联网 发布:mysql时间带毫秒 编辑:程序博客网 时间:2024/06/08 12:18

 

1.grep基本概念和命令格式:

grep用于在文件中全局查找指定的正则表达式,并把符合查找条件的行全部打印出来。

我们看下grep的全称就完全明白它是干什么的了:GlobalRegularExpressionPrint

print自然就是把查找结果打印到屏幕,当然你也可以重定向到文件。

有了grep之后,就不再需要像VI那样打开文件才能查找,也不需要把正则表达式括在两个正斜杠之间了。

 

1.1. grep的一般格式:

grepRE filename

[root@localhost travel_life]#grepworldaaa.txt

A thousand years later, the world woke up from your dream

[root@localhost travel_life]#

 

可以一次查询多个文件,第一个参数是模式,后面所有参数都是文件名:

 [root@localhost travel_life]# grep 'm$' a*.txt

aaa.txt: A thousand years later, the world woke up from yourdream

asdf.txt:in dream

[root@localhost travel_life]#

 

注意如果查找的串是多个字符,那么一定要用单引号或双引号:

[root@localhost travel_life]# grep'A thousand'aaa.txt

A thousand years later, the world woke up from your dream

[root@localhost travel_life]#

 

否则,就会把thousand也当成一个文件,并且仅仅在文件中查找“A”,而非你想要的“A thousand”:

[root@localhost travel_life]# grep A thousandaaa.txt

grep: thousand: No such file or directory

aaa.txt:A chinese ghost

aaa.txt:Athousand years later, the world woke up from your dream

 

1.2.grep总会返回一个执行结果状态值:

0表示查找到结果;1表示未查找到;2表示文件不存在:

------- clip-------

[root@localhost travel_life]# grep-n 'me$' aaa.txt

9:decided that it would no longer be rhyme

[root@localhost travel_life]# echo $?

0

[root@localhost travel_life]# grep -n 'mem$' aaa.txt

[root@localhost travel_life]# echo $?

1

[root@localhost travel_life]# grep -n 'mem$' aaab.txt

grep: aaab.txt: No such file or directory

[root@localhost travel_life]# echo $?

2

------- clip-------

 

2.grep命令中使用正则表达式:

匹配行尾:

[root@localhost travel_life]# grep 'm$' aaa.txt

A thousand years later, the world woke up from your dream

 

使用转义符:

先看这个文件的内容,有四处用到了.符号:

 [root@localhost travel_life]# cat myblog.txt

hi, my blog is http://blog.csdn.net/travel_life

what's the difference between.netand.com?

I don't know, but I can google why

but I prefer to .org, wa haha

 

查找.后面包含n的行:

[root@localhost travel_life]# grep'\.n'myblog.txt

hi, my blog is http://blog.csdn.net/travel_life

what's the difference between.netand .com?

 

查找.后面跟一个任意字符的行:

 [root@localhost travel_life]# grep'\..'myblog.txt

hi, my blog is http://blog.csdn.net/travel_life

what's the difference between.net and.com?

but I prefer to .org, wa haha

 

 

匹配行尾以?或y结尾的行:

[root@localhost travel_life]# grep'[?y]$'myblog.txt

what's the difference between .net and .com?

I don't know, but I can google why

[root@localhost travel_life]#

 

只要某一行有非大写字母就打印,所以全部文件被打印:

[root@localhost travel_life]# grep'[^A-Z]'aaa.txt  

A chinese ghost

wandering in the maze lonely

A thousand years later, the world woke up from your dream

and strengthed itself

decided that it would no longer be rhyme

 

查找有数字的行:

[root@localhost travel_life]# grep'[0-9]'aaa.txt

[root@localhost travel_life]#

 

包含一个大写字母,后面跟一个空格,然后再跟一个小写字母的行:

[root@localhost travel_life]# grep'[A-Z] [a-z]'aaa.txt

A chinese ghost

A thousand years later, the world woke up from your dream

[root@localhost travel_life]#

 

 

查找5个字母单词的行:

[root@localhost travel_life]# cat aaa.txt

A chinese ghost

wandering in the maze lonely

A thousand years later, the world woke up from your dream

and strengthed itself

decided that it would no longer be rhyme

[root@localhost travel_life]#

 

[root@localhost travel_life]# grep'\<[a-z]\{5\}\>'aaa.txt

A chinese ghost

A thousand years later, the world woke up from your dream

decided that it would no longer be rhyme

[root@localhost travel_life]#

 

查找e后面跟0到多个d,再跟一个空格的行:

[root@localhost travel_life]# grep'ed* 'aaa.txt

A chineseghost

wandering in the maze lonely

A thousand years later, the worldwokeup from your dream

and strengthed itself

decidedthat it would no longer be rhyme

 

标签的应用:

[root@localhost travel_life]# cat bbb.txt

Drama is not chinse ma but a ma?

Don't know what the key factors Drama consists of

But still you can google it

Don't you?

 

查找含有一个ma然后跟0到多个字符,然后跟空格,再跟一个ma的行:

[root@localhost travel_life]# grep'\(ma\>\) .* \1'bbb.txt

Drama is not chinsema but a ma?

 

查找含有一个ma然后跟任意多个字符,然后跟空格,再跟0到多个ma的行:

[root@localhost travel_life]# grep'\(ma\>\) .* \1*'bbb.txt

Drama is not chinse ma but a ma?

Don't know what the key factorsDramaconsists of

[root@localhost travel_life]#

 

3. grep选项的应用:

3.1. -i,关闭大小写。

如下例子中的 th可以大小写任意组合:

[root@localhost travel_life]# cat ccc.txt

The most important thing is:

to listen well and sound good

isn't is it, does that make sense?

Are you clear about "THE"'s meaning?

yes, maybe, although it is difficult to me.

 

[root@localhost travel_life]# grep-i'\<th' ccc.txt

Themost importantthingis:

isn't is it, does that make sense?

Are you clear about "THE"'s meaning?

 

3.2. -v,不查找符合条件的行:

[root@localhost travel_life]# cat ddd.txt

Lisa is my friend

Lili is not my friend

Lisa always hang out with me at weekends

But Lili is not around usually and don't want be  with us

 

然后我想把关于Lili的行都删掉,再把输出结果重定向到另一个文件:

[root@localhost travel_life]# grep-v 'Lili'ddd.txt > myfriend.txt

[root@localhost travel_life]# cat myfriend.txt

Lisa is my friend

Lisa always hang out with me at weekends

 

但是不能重定向到自己,否则会把本身文件给毁了:
[root@localhost travel_life]# grep -v 'Lili' ddd.txt > ddd.txt

[root@localhost travel_life]# cat ddd.txt

[root@localhost travel_life]#

 

3.3. -l,只输入符合查找条件的文件名而不输出具体的文本行:

[root@localhost travel_life]# grep-l '\<the\>' *.txt

aaa.txt

bbb.txt

myblog.txt

[root@localhost travel_life]#

 

3.4. -c,统计含有查找模式的行的数量:

[root@localhost travel_life]# cat aaa.txt

A chinese ghost

wandering in the maze lonely

A thousand years later, the world woke up from your dream

and strengthed itself

decided that it would no longer be rhyme

 

[root@localhost travel_life]# grep-c 'at'aaa.txt

2

 

3.5. -w,使模式只作为词而非词的一部分被查找:

接上面的例子,加上 –w之后,’at’就查不到了,因为文件里没有at这个单词:

[root@localhost travel_life]# grep-w 'at'aaa.txt

 

必须要用单词的完全拼写:

[root@localhost travel_life]# grep-w 'that' aaa.txt

decided that it would no longer be rhyme

[root@localhost travel_life]#

 

3.6.打印shell的环境变量,即使shell变量被括在双引号之间也无影响:        

[root@localhost travel_life]# VERB=woke

[root@localhost travel_life]# echo $VERB

woke

 [root@localhost travel_life]# grep -i $VERB aaa.txt

A thousand years later, the world woke up from your dream

 [root@localhost travel_life]# grep -i "$VERB" aaa.txt

A thousand years later, the world woke up from your dream

 

4. grep与管道:

查找所有在十二月份建立的文件(非目录)

[root@localhost home]# ll | grep '^[^d].*\<Dec\>'

-rwxrwxrwx  1 root      root        29Dec31  2011 off_license_list_R3

-rwxrwxrwx  1 root      root      1739Dec15  2011 setup_resiliency.sh

[root@localhost home]#

0 0
原创粉丝点击