shell脚本工具之grep命令

   grep(缩写来自Globally search a Regular Expression and Print)是Linux系统的一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.egrep和fgrep都是grep的扩展,支持更多的re元字符,fgrep就是fixed grep或fast grep.linux使用GNU版本的grep,它功能更强,可以通过-G、-E、-F命令行选项来使用egrepfgrep的功能.grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2.我们利用这些返回值就可进行一些自动化的文本处理工作.


grep的正规表达式元字符

^              行首定位符

$              行尾定位符

.              匹配任意一个字符

*              匹配0个或多个前导字符

[]             匹配指定范围内的其中一个字符

[^]            匹配不要范围内的字符

\<             词首定位符

/〉            词尾定位符

x\{m\}         重复x字符m次

x\{m,\}        重复x字符最少m次

x\{m,n\}       重复x字符m到n次


文件内容:

[root@tong1 opt]# ll passwd
-rw-r--r--. 1 root root 1087 Mar 19 17:39 passwd
[root@tong1 opt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@tong1 opt]# 


1.grep命令格式

grep [选项] 字符模式 [文件名1,文件名2.........]


2.查找r开头的行

[root@tong1 opt]# grep ‘^r‘ passwd
root:x:0:0:root:/root:/bin/bash
[root@tong1 opt]#


3.查找以c结尾的行

[root@tong1 opt]# grep ‘c$‘ passwd
sync:x:5:0:sync:/sbin:/bin/sync
[root@tong1 opt]#


4.查找以h开头,t结尾,中间只有两个字符的行

[root@tong1 opt]# grep ‘\<h..t\>‘ passwd
halt:x:7:0:halt:/sbin:/sbin/halt
[root@tong1 opt]# 


5.查找文件内容只要有h或q的字符

[root@tong1 opt]# grep ‘[hq]‘ passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
[root@tong1 opt]# 


6.查找每行有a到o字符出现7次的

[root@tong1 opt]# grep ‘[a-o]\{7\}‘ passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@tong1 opt]# 


7.在一些文件中查找相同的内容

[root@tong1 opt]# grep root passwd*
passwd:root:x:0:0:root:/root:/bin/bash
passwd:operator:x:11:0:operator:/root:/sbin/nologin
passwd1:root:x:0:0:root:/root:/bin/bash
passwd1:operator:x:11:0:operator:/root:/sbin/nologin
[root@tong1 opt]#


8.显示grep结果的行号

[root@tong1 opt]# grep root -n passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
[root@tong1 opt]#


9.显示包含字符的文件名

[root@tong1 opt]# grep root -l passwd
passwd
[root@tong1 opt]#


10.显示文件中的字符

[root@tong1 opt]# grep root -c passwd
2
[root@tong1 opt]# 


11.查找内容是单词

[root@tong1 opt]# grep -w halt passwd
halt:x:7:0:halt:/sbin:/sbin/halt
[root@tong1 opt]#


12.反向过滤

[root@tong1 opt]# grep -v bash passwd | grep -v nologin passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@tong1 opt]#

13.用-E解释通配符

[root@tong1 opt]# grep -v -E ‘bash|nologin‘ passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@tong1 opt]#

本文出自 “一起走过的日子” 博客,请务必保留此出处http://tongcheng.blog.51cto.com/6214144/1622319

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。