正則介紹_grep

10月16日任務java

9.1 正則介紹_grep上linux

9.2 grep中centos

9.3 grep下bash

 

grep基本用法

grep [-cinvABC] 'word' filename測試

  • centos7.x版本的grep實則爲grep --color=auto
  • -c 顯示匹配到的行數
[root@localhost ~]# cp /etc/passwd /tmp
[root@localhost ~]# cd /tmp
[root@localhost tmp]# 

[root@centos7 tmp]# grep -c 'root' passwd 
2
  • -i 忽略大小寫
[root@centos7 tmp]# grep -i castiel passwd
castiel:x:1000:1000::/home/castiel:/bin/bash
Castiel:X:1000:1000::/home/castiel:/bin/bash
  • -n 顯示匹配行的同時顯示行號
[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
  • -v 對匹配信息取反
[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
.....
  • -r 遞歸匹配過濾(對目錄進行操做)
[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
  • -An(after) 顯示匹配行及其後n行
[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
  • -Bn(before) 顯示匹配行及其前n行
[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
  • -Cn 顯示匹配行及其先後n行
[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
  • 匹配任意1個字符 .
[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
  • 匹配0或多個以前的一個字符 *
[root@localhost ~]# grep 'a*m' test.txt 
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

擴展正則(grep -E 或 egrep)

  • 匹配1或多個以前的一個字符 +
[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個以前的字符 ?
# 匹配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實例

  • 過濾註釋行 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
  • 過濾出/etc目錄下,包含root字符的全部的.conf文件內的行!--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
相關文章
相關標籤/搜索