虚拟化容器,大数据,DBA,中间件,监控。

Shell 四剑客之 grep高效查找

21 12月
作者:admin|分类:脚本编程

 

‘Linux系统中的grep命令是一种功能强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

 

语法参数


Grep常用参数详解如下:

-a  以文本文件方式搜索(默认参数)

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

-i   忽略大小写

-n  顺便输出行号

-v 反向选择,即显示不包含匹配文本的所有行

-h 查询多文件时不显示文件名

-l 查询多文件时只输出包含匹配字符的文件名

-s 不显示不存在或无匹配文本的错误信息

-E 允许使用egrep扩展模式匹配

-o    表示“only-matching”,即“仅匹配”之意(获取匹配到的字符串)

 

实战使用grep过滤关键字


使用grep在单个文件检索 

[root@www ~]# cat score.txt 
Aaron    Physics    87
Abel     Maths      98
Rahul    Chinese    90
Buck     Biology    87
Byron    English    85
Dave     History    89
Enoch    Chemistry  89

#查找包含“87”的行
[root@www ~]# grep 87 score.txt 
Aaron    Physics    87
Buck     Biology    87


#查找不包含87的行并且打印行号
[root@www ~]# grep -vn 87 score.txt 
2:Abel     Maths      98
3:Rahul    Chinese    90
5:Byron    English    85
6:Dave     History    89
7:Enoch    Chemistry  89

# -n选项打印行号
[root@localhost ~]# grep -n '87' score.txt 
1:Aaron    Physics    87
4:Buck     Biology    87
[root@localhost ~]# grep -n '87' score.txt | awk -F'[: ]' '{print $1,$NF}'
1 87
4 87


#把所有的内容都打印出来
[root@www ~]# grep -n '' score.txt   
1:Aaron    Physics    87
2:Abel     Maths      98
3:Rahul    Chinese    90
4:Buck     Biology    87
5:Byron    English    85
6:Dave     History    89
7:Enoch    Chemistry  89


# -o选项 不会将行打印出来,只打印匹配到的
[root@www ~]# grep -o Buck score.txt 
Buck



#统计文本里面匹配到不区分大小写abel,然后统计次数出现多少次
[root@www ~]# grep -aci 'abel' score.txt 
1

#忽略大小写i v不匹配
[root@localhost ~]# grep -avi  rahul score.txt 
Aaron    Physics    87
Abel     Maths      98
Buck     Biology    87
Byron    English    85
Dave     History    89
Enoch    Chemistry  89


#grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。
[root@www ~]# grep "static" /etc/sysconfig/network-scripts/ifcfg-ens33 
BOOTPROTO="static"
[root@www ~]# echo $?
0
[root@www ~]# grep "dhcp" /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@www ~]# echo $?
1
通过grep和if实现某些判断条件
[root@www ~]# num=$(grep static /etc/sysconfig/network-scripts/ifcfg-ens33|wc -l)
[root@www ~]# echo $num
1

grep在同一目录下多文件关键字的检索

[root@localhost ~]# cp score.txt score.txt1
[root@localhost ~]# cp score.txt score.txt2

#可以从多个文件检索
[root@localhost ~]# grep -i aaron score.txt score.txt1 score.txt2
score.txt:Aaron    Physics    87
score.txt1:Aaron    Physics    87
score.txt2:Aaron    Physics    87
或者
[root@localhost ~]# grep -i aaron score.txt*
score.txt:Aaron    Physics    87
score.txt1:Aaron    Physics    87
score.txt2:Aaron    Physics    87

#查询多个文件不显示文件名
[root@localhost ~]# grep -ih aaron score.txt*
Aaron    Physics    87
Aaron    Physics    87
Aaron    Physics    87

#只看文件名,查询到文件名可以使用for循环然后使用sed替换Aaron的值
[root@localhost ~]#  grep -l  'Aaron' score.txt*  
score.txt
score.txt1
score.txt2
[root@localhost ~]# for i in `grep -l  'Aaron' score.txt*`;do echo $i;done  
score.txt
score.txt1
score.txt2
[root@localhost ~]# for i in `grep -l  'Aaron' score.txt*`;do sed s/Aaron/replace/g $i;done  
replace    Physics    87
Abel     Maths      98
Rahul    Chinese    90
Buck     Biology    87
Byron    English    85
Dave     History    89
Enoch    Chemistry  89
replace    Physics    87
Abel     Maths      98
Rahul    Chinese    90
Buck     Biology    87
Byron    English    85
Dave     History    89
Enoch    Chemistry  89
replace    Physics    87
Abel     Maths      98
Rahul    Chinese    90
Buck     Biology    87
Byron    English    85
Dave     History    89
Enoch    Chemistry  89


#查看关键字出现次数
[root@www ~]# grep -aci  aaron  socre.txt  socre1.txt socre2.txt 
socre.txt:1
socre1.txt:1
socre2.txt:1

grep在多级目录下多文件关键字的检索

[root@localhost ~]# grep -i aaron *
grep: a: Is a directory
grep: b: Is a directory
grep: c: Is a directory
score1.txt:Aaron    Physics    87
score2.txt:Aaron    Physics    87
score.txt:Aaron    Physics    87

可以看到如果目录分级了就过滤不了关键字aaron需要使用-r
[root@localhost ~]# grep -ir aaron *
a/score.txt:Aaron    Physics    87
b/score.txt:Aaron    Physics    87
c/score.txt:Aaron    Physics    87
score1.txt:Aaron    Physics    87
score2.txt:Aaron    Physics    87
score.txt:Aaron    Physics    87
[root@localhost ~]# grep -irl aaron *
a/score.txt
b/score.txt
c/score.txt
score1.txt
score2.txt
score.txt

批量替换将aaron替换为test
[root@localhost ~]# for i in `grep -irl aaron *`;do sed -n 's/Aaron/test/p' $i;done
test    Physics    87
test    Physics    87
test    Physics    87
test    Physics    87
test    Physics    87
test    Physics    87

 

浏览692 评论0
返回
目录
返回
首页
Keepalived Shell脚本来简单实现keepalived功能 Shell 四剑客之grep(1)