Linux 上時常需要對文本或命令輸出做過濾,最常用的莫過於 grep。
grep [OPTIONS] PATTERN [FILE...]
grep 會逐行檢索輸入,若該行包含模式 PATTERN 就輸出。這裡的 PATTERN 是正規表達式。
# 從檔案中找含 root 的行 [root@centos7 temp]# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin # 從標準輸入取得 [root@centos7 temp]# cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
值得注意的一點:當 grep 同時收到檔案與標準輸入時,它會忽略標準輸入,除非用 - 明確代表標準輸入:
[root@centos7 temp]# cat /etc/passwd | grep root /etc/passwd - /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin (standard input):root:x:0:0:root:/root:/bin/bash (standard input):operator:x:11:0:operator:/root:/sbin/nologin
# 以 root 開頭 [root@centos7 temp]# grep "^root" /etc/passwd /etc/group /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/group:root:x:0: # 以 /bin/bash 結尾 [root@centos7 temp]# grep "/bin/bash$" /etc/passwd root:x:0:0:root:/root:/bin/bash learner:x:1000:1000::/home/learner:/bin/bash
以上兩例的 PATTERN 都用雙引號包起來,避免被 shell 提前解析。
# 不以 a-s 任一字元開頭 [root@centos7 temp]# grep "^[^a-s]" /etc/passwd
要理解兩個 ^ 的不同含義:第一個表示行首,第二個位在 [] 內的首位時表示取反。
# 字元 0 連續出現 3 次以上(注意轉義)
[root@centos7 temp]# grep "0\{3,\}" /etc/passwd
learner:x:1000:1000::/home/learner:/bin/bash
# 以 r 或 l 開頭
[root@centos7 temp]# grep "^[r,l]" /etc/passwd
# -i 忽略大小寫
[root@centos7 temp]# grep -i abcd file
ABCD
function abcd() {
# -o 只輸出匹配的字元,而非整行
[root@centos7 temp]# grep -oi abcd file
ABCD
abcd
# -c 統計匹配行數
[root@centos7 temp]# grep -oic abcd file
2
# -v 反向匹配
[root@centos7 temp]# grep -v "/sbin/nologin$" /etc/passwd
# -f FILE 以檔案中每行作為模式
[root@centos7 temp]# grep -f test file
ABCD
function abcd() {
# -x 整行匹配
[root@centos7 temp]# grep -xf test file
ABCD
# -w 匹配整個單字
[root@centos7 temp]# grep here file
here
there
[root@centos7 temp]# grep -w here file
here
# -h 多檔案時不輸出檔名
[root@centos7 temp]# cat /etc/passwd | grep ^root -h
root:x:0:0:root:/root:/bin/bash
# -n 顯示行號
[root@centos7 temp]# grep -n "^[r,l]" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
24:learner:x:1000:1000::/home/learner:/bin/bash
-A N 輸出匹配行及其「之後」的 N 行 -B N 輸出匹配行及其「之前」的 N 行 -C N 輸出匹配行及其前後各 N 行
[root@centos7 temp]# grep -A 2 ^operator /etc/passwd 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 temp]# grep -B2 ^operator /etc/passwd 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 temp]# grep -C1 ^operator /etc/passwd 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
-F 把 PATTERN 當字面值比對(忽略字元特殊含義),等同 fgrep:
[root@centos7 temp]# grep -F ^root /etc/passwd (無輸出)
-E 使用擴展正規表達式,等同 egrep。好處是 ?, +, {, |, (, ) 不必轉義就有特殊含義:
[root@centos7 temp]# egrep "^root|^learner" /etc/passwd root:x:0:0:root:/root:/bin/bash learner:x:1000:1000::/home/learner:/bin/bash
-P 使用 Perl 正規表達式:
[root@centos7 ~]# echo "helloworld123456" | grep -oP "\d+" 123456
Perl 正則中 \d 代表數字,+ 表示匹配一到多次。
-a 把二進位檔當文本處理:
[root@centos7 ~]# grep -a online /usr/bin/ls %s online help: <%s>
--exclude=GLOB / --include=GLOB 分別排除與包含符合通配符的檔案:
[root@centos7 temp]# find . -type f | xargs grep --exclude=*.txt --include=test* bash ./test.sh:#!/bin/bash
grep 的過濾力來自選項與正規表達式的搭配組合,後續文章會有更多變化。