您的位置:首页 > 运维架构 > Linux

每天一个Linux命令之三剑客grep

2017-07-23 09:11 447 查看

每天一个Linux命令之三剑客grep

grep:打印出相匹配的行

语法:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
选项:匹配控制-e 使用模式匹配,可以指定多个匹配模式

-f FILE 从文件的每一行获得匹配模式

-i 忽略大小写

-v 反向打印,即打印不匹配的行

-w 模式匹配整个单词

-x 模式匹配整行

输出控制-c 只打印整个文件匹配的行数

-o 只打印匹配的内容

-q 不输出正常的信息

-s 不输出错误的信息

内容行控制-A NUM, --after-context=NUM 打印匹配的后几行

-B NUM, --before-context=NUM 打印匹配的前几行

-C NUM, -NUM, --context=NUM 打印匹配的前后几行

重复匹配说明c 匹配字母c

. 匹配任意单个字母

* 匹配前一个字母出现零次或者多次

.* 匹配多个字符

【】匹配括号中的任意单个字符

【x-y】匹配连续的字符串范围

^ 匹配字符串的开头

$ 匹配字符串的结尾

【^】 对括号内的字符取反

\{n\} 匹配前一个字符重复n次

\{n,m\} 匹配前一个字符重复n到m次

\{n,\} 匹配前一个字符至少重复n次

\(\) 将括号中的字符储存在保留空间

\n 调用保留空间的内容(即调用上\(\))c储存在保留空间的内容)

例子:本例程使用到的文本
[root@python ~]# cat test.txt
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
打印出相匹配的内容
[root@python ~]# grep 'blp5' test.txt
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator

# .. 在这里占两个字符
[root@python ~]# grep 'b..5' test.txt
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator

【】的用法
#打印出含有485和486的行
[root@python ~]# grep '48[56]' test.txt
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject

#打印出所有含有数字的行
[root@python ~]# grep '[0-9]' test.txt
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject

#打印出含有数字1,2,3的行
[root@python ~]# grep '[1-3]' test.txt
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
iqobject        48619/tcp               # iqobject

#打印出含有字母x,y,z的行
[root@python ~]# grep '[x-z]' test.txt
isnetserv       48128/udp               # Image Systems Network Services

匹配开头和结尾的行
#匹配以3gpp开头的行
[root@python ~]# grep '^3gpp' test.txt
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol

#匹配以dw结尾的行
[root@python ~]# grep 'dw$' test.txt
com-bardac-dw   48556/udp               # com-bardac-dw

匹配取反
#匹配9/后面不是tcp的行(注意区分^在括号内:代表取反。在括号外代表以什么开头)
[root@python ~]# grep '9/[^tcp]' test.txt
blp5            48129/udp               # Bloomberg locator

#匹配没有9/tcp的行
[root@python ~]#  grep -v '9/tcp' test.txt
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/udp               # com-bardac-dw

. * .*匹配模式
[root@python ~]# cat test1.txt
google
gooogle
goooogle
gooooole

#打印gg中间占两个字符的行
[root@python ~]# grep 'g..g' test1.txt
google

#打印含有gooo的行
[root@python ~]# grep 'goooo*' test1.txt
gooogle
goooogle
gooooole

#打印含有goooo的行
[root@python ~]# grep 'goooo.*' test1.txt
goooogle
gooooole

精确匹配前一个字符出现的次数
#匹配o出现两次
[root@python ~]# grep 'go\{2\}gle'  test1.txt
google

#匹配o至少出现两次
[root@python ~]# grep 'go\{2,\}gle'  test1.txt
google
gooogle
goooogle

#匹配o出现2到3次
[root@python ~]# grep 'go\{2,3\}gle'  test1.txt
google
gooogle

#上面的结果可以用扩展正则来代替,就不用使用转义字符
[root@python ~]# egrep 'go{2}gle'  test1.txt
google
[root@python ~]# egrep 'go{2,}gle'  test1.txt
google
gooogle
goooogle
[root@python ~]# egrep 'go{2,3}gle'  test1.txt
google
gooogle

打印匹配的前(B)后(A)以及前后行(C)
#打印匹配的后两行
[root@python ~]# grep  -A 2 'blp5' test.txt
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject

#打印匹配的前两行
[root@python ~]# grep  -B 2 'blp5' test.txt
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator

#打印匹配的前后两行
[root@python ~]# grep  -C 2 'blp5' test.txt
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject

打印匹配的行数和内容
#只打印匹配的行数
[root@python ~]# seq 9|grep -c '[1-5]'
5

#只打印匹配的内容
[root@python ~]# seq 9|grep -o '[1-5]'
1
2
3
4
5

#打印出总行数
[root@python ~]# seq 9|grep -c '$'
9

忽略大小写(-i)
[root@python ~]# echo -e 'AA\naa\naaa'|grep -i 'a'
AA
aa
aaa

匹配多个内容(-e)
[root@python ~]# grep -e 'blp5' -e '3gpp' test.txt
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator

过滤文件的空白行和文件的非空白行
#过滤文件的空白行
[root@python ~]# grep '^$' test1.txt

#过滤出文件的非空白行
[root@python ~]# grep -v '^$' test1.txt
google
gooogle
goooogle
gooooole

输出两个文件中相同的行
[root@python ~]# cat a.txt
hello
world
what
who
jack
[root@python ~]# cat b.txt
hello
world
what
which
jim
[root@python ~]# grep -f a.txt b.txt
hello
world
what

匹配IP地址
[root@python ~]# ifconfig eth0|grep 'Bcast'|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
192.168.1
192.168.1
255.255.255

[root@python ~]# ifconfig eth0|grep 'Bcast'|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'|head -n1
192.168.1

2017/4/24 10:27:05
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  grep