shell脚本工具之grep命令

本文发布时间: 2019-Mar-22
grep(缩写来自Globally search a Regular Expression and Print)是Linux系统的一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.egrep和fgrep都是grep的扩展,支持更多的re元字符,fgrep就是fixed grep或fast grep.linux使用GNU版本的grep,它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能.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/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp: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/bashshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltgopher: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/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp: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/bashpasswd:operator:x:11:0:operator:/root:/sbin/nologinpasswd1:root:x:0:0:root:/root:/bin/bashpasswd1:operator:x:11:0:operator:/root:/sbin/nologin[root@tong1 opt]# 8.显示grep结果的行号[root@tong1 opt]# grep root -n passwd1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator:/root:/sbin/nologin[root@tong1 opt]# 9.显示包含字符的文件名[root@tong1 opt]# grep root -l passwdpasswd[root@tong1 opt]# 10.显示文件中的字符[root@tong1 opt]# grep root -c passwd2[root@tong1 opt]#11.查找内容是单词[root@tong1 opt]# grep -w halt passwdhalt:x:7:0:halt:/sbin:/sbin/halt[root@tong1 opt]# 12.反向过滤[root@tong1 opt]# grep -v bash passwd | grep -v nologin passwdroot:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt[root@tong1 opt]# 13.用-E解释通配符[root@tong1 opt]# grep -v -E 'bash|nologin' passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt[root@tong1 opt]#


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:09pm
栏目列表