1.grep是文本过滤工具,在搜索字符串时以行为单位。
基本用法:
grep参数’字符串‘文件
常用的选项有:
[root@zhu1 ~]# grep 'root' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@zhu1 ~]# grep -c 'root' passwd 2
-i:搜索时忽略大小写
[root@zhu1 ~]# grep 'user' passwd rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin [root@zhu1 ~]# grep -i 'user' passwd ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin ldap:x:55:55:LDAP User:/var/lib/ldap:/bin/false
[root@zhu1 ~]# grep 'root' passwd /etc/passwd passwd:root:x:0:0:root:/root:/bin/bash passwd:operator:x:11:0:operator:/root:/sbin/nologin /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin [root@zhu1 ~]# grep -h 'root' passwd /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
-l:只列出符合条件的文件名,不列出具体的匹配行
[root@zhu1 ~]# grep 'root' passwd zhu.txt passwd:root:x:0:0:root:/root:/bin/bash passwd:operator:x:11:0:operator:/root:/sbin/nologin [root@zhu1 ~]# grep -l 'root' passwd zhu.txt passwd
-n:列出所有的匹配行,并显示行号
[root@zhu1 ~]# grep 'root' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@zhu1 ~]# grep -n 'root' passwd 1:root:x:0:0:root:/root:/bin/bash 12:operator:x:11:0:operator:/root:/sbin/nologin
[root@zhu1 ~]# grep 'root' passwd.txt grep: passwd.txt: 没有那个文件或目录 [root@zhu1 ~]# grep -s 'root' passwd.txt [root@zhu1 ~]#
-v:显示不包含匹配字符串的所有行
[root@zhu1 ~]# grep -n 'root' passwd 1:root:x:0:0:root:/root:/bin/bash 12:operator:x:11:0:operator:/root:/sbin/nologin [root@zhu1 ~]# grep -nv 'root' passwd 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:news:x:9:13:news:/etc/news: 11:uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 13:games:x:12:100:games:/usr/games:/sbin/nologin
[root@zhu1 ~]# grep -l 'root' * passwd [root@zhu1 ~]# grep -rl 'root' * packages/libiconv-1.14/Makefile packages/libiconv-1.14/libtool packages/libiconv-1.14/Makefile.in packages/libiconv-1.14/doc/relocatable.texi packages/libiconv-1.14/lib/libcharset.so.1.0.0
-w:匹配整个词
[root@zhu1 ~]# grep an zhu.txt ba jiu wen qing tian. bu zhi tian shang gong que. jin xi shi he nian root oo* an good man oo$ [root@zhu1 ~]# grep -w an zhu.txt an good man oo$
-x:匹配整行
-E:支持扩展的正则表达式
[root@zhu1 ~]# grep 'zhu' jiang.txt zhu zhuzhu [root@zhu1 ~]# grep -x 'zhu' jiang.txt zhu [root@zhu1 ~]#
-F:不支持正则表达式,按照字符的字面意思进行匹配
2.grep与正则表达式结合
1.在zhu.txt文件中查找含有字符串an的行
[root@zhu1 ~]# grep 'an' zhu.txt ba jiu wen qing tian bu zhi tian shang gong que goad #jin xi shi he nian root oo* an good man
2.在zhu.txt文件中查找以an开头的行
[root@zhu1 ~]# grep '^an' zhu.txt an good man
3.在zhu.txt中含有以an结尾的行
[root@zhu1 ~]# grep 'an$' zhu.txt ba jiu wen qing tian
[root@zhu1 ~]# nl zhu.txt 1 ming yue ji shi you. 2 ba jiu wen qing tian 3 bu zhi tian shang gong que goad 4 #jin xi shi he nian root oo* 5 #an good man [root@zhu1 ~]# grep -v '^$' zhu.txt | grep -v '^#' zhu.txt ming yue ji shi you. ba jiu wen qing tian bu zhi tian shang gong que goad
[root@zhu1 ~]# egrep -v '^$|^#' zhu.txt ming yue ji shi you. ba jiu wen qing tian bu zhi tian shang gong que goad原文链接:https://www.f2er.com/regex/362649.html