10月16日任務java
9.1 正則介紹_grep上linux
9.2 grep中centos
9.3 grep下bash
grep [-cinvABC] 'word' filename測試
[root@localhost ~]# cp /etc/passwd /tmp [root@localhost ~]# cd /tmp [root@localhost tmp]# [root@centos7 tmp]# grep -c 'root' passwd 2
[root@centos7 tmp]# grep -i castiel passwd castiel:x:1000:1000::/home/castiel:/bin/bash Castiel:X:1000:1000::/home/castiel:/bin/bash
[root@centos7 tmp]# grep -n 'root' passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
[root@centos7 tmp]# grep -iv castiel 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 .....
[root@centos7 tmp]# grep -r 'root' /etc /etc/pki/ca-trust/ca-legacy.conf:# The upstream Mozilla.org project tests all changes to the root CA /etc/pki/ca-trust/ca-legacy.conf:# to temporarily keep certain (legacy) root CA certificates trusted, /etc/pki/ca-trust/ca-legacy.conf:# It may keep root CA certificate as trusted, which the upstream /etc/pki/ca-trust/extracted/README:root CA certificates. /etc/pki/ca-trust/extracted/java/README:root CA certificates. 匹配到二進制文件 /etc/pki/ca-trust/extracted/java/cacerts /etc/pki/ca-trust/extracted/openssl/README:root CA certificates. /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt:# Comodo AAA Services root /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt:# Comodo Secure Services root /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt:# Comodo Trusted Services root
[root@centos7 tmp]# grep -A2 'root' 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 -- operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@centos7 tmp]# grep -B2 'root' passwd root:x:0:0:root:/root:/bin/bash -- halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
[root@centos7 tmp]# grep -C2 'root' 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 -- halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
# 測試內容 [root@localhost ~]# cat test.txt 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 111111111111111111111111111111 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa #abcskhd
[0-9]
[root@localhost ~]# grep '[0-9]' test.txt 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 111111111111111111111111111111
[a-zA-Z]
[root@localhost ~]# grep '[a-zA-Z]' test.txt 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 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa #abcskhd
[^]
(在方括號內才表示取反)[root@localhost ~]# grep '[^a-zA-Z]' test.txt 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 111111111111111111111111111111 #abcskhd
^
[root@localhost ~]# grep '^#' test.txt #abcskhd
$
[root@localhost ~]# grep 'in$' test.txt 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
.
[root@localhost ~]# grep 'b.n' test.txt 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
[root@localhost ~]# grep 'a*m' test.txt daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
+
[root@localhost ~]# grep -E 'ro+t' test.txt root:x:0:0:root:/root:/bin/bash
{n}
或 {n, m}
[root@localhost ~]# grep -E 'o{2}' test.txt root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@localhost ~]# grep -E 'o{1,2}' test.txt 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
?
# 匹配0或1個字符o [root@localhost ~]# grep -E 'oo?' test.txt 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
|
[root@localhost ~]# grep -E '111|aaa' test.txt 111111111111111111111111111111 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
grep -v '^#'
grep -v '^$'
grep -E '+86-[0-9]{11}'
[root@centos7 tmp]# echo "+86-15301203401" | grep -E '+86-[0-9]{11}' +86-15301203401
grep -E '\.\*@\.\*\.\.\*'
[root@centos7 tmp]# echo "12ab3#@123@qq.com" | grep -E '.*@.*\..*' 12ab3#@123@qq.com [root@centos7 tmp]# echo "12323@163.com" | grep -E '.*@.*\..*' 12323@163.com
--include
[root@centos7 tmp]# grep -r --include="*.conf" 'root' /etc /etc/pki/ca-trust/ca-legacy.conf:# The upstream Mozilla.org project tests all changes to the root CA /etc/pki/ca-trust/ca-legacy.conf:# to temporarily keep certain (legacy) root CA certificates trusted, /etc/pki/ca-trust/ca-legacy.conf:# It may keep root CA certificate as trusted, which the upstream /etc/yum/pluginconf.d/fastestmirror.conf:# as root). /etc/selinux/semanage.conf:ignoredirs=/root /etc/dbus-1/system.d/teamd.conf: <policy user="root"> /etc/dbus-1/system.d/org.freedesktop.hostname1.conf: <policy user="root"> /etc/dbus-1/system.d/org.freedesktop.import1.conf: <policy user="root"> /etc/dbus-1/system.d/org.freedesktop.locale1.conf: <policy user="root"> ......
[root@localhost ~]# grep -E 'a(e|d)m' test.txt daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin