Linux中國
14 個 grep 命令的例子
在本文中,我們將會討論到14個grep命令的例子。
例1 在文件中查找模式(單詞)
在/etc/passwd文件中查找單詞「linuxtechi」
root@Linux-world:~# grep linuxtechi /etc/passwd
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
例2 在多個文件中查找模式。
root@Linux-world:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/shadow:linuxtechi:$6$DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7:::/etc/gshadow:adm:*::syslog,linuxtechi
/etc/gshadow:cdrom:*::linuxtechi
/etc/gshadow:sudo:*::linuxtechi
/etc/gshadow:dip:*::linuxtechi
/etc/gshadow:plugdev:*::linuxtechi
/etc/gshadow:lpadmin:!::linuxtechi
/etc/gshadow:linuxtechi:!::
/etc/gshadow:sambashare:!::linuxtechi
root@Linux-world:~#
例3 使用-l參數列出包含指定模式的文件的文件名。
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
/etc/passwd
/etc/shadow
root@Linux-world:~#
例4 使用-n參數,在文件中查找指定模式並顯示匹配行的行號
root@Linux-world:~# grep -n linuxtechi /etc/passwd
39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
root@Linux-world:~# grep -n root /etc/passwd /etc/shadow

例5 使用-v參數輸出不包含指定模式的行
輸出/etc/passwd文件中所有不含單詞「linuxtechi」的行
root@Linux-world:~# grep -v linuxtechi /etc/passwd

例6 使用 ^ 符號輸出所有以某指定模式開頭的行
Bash腳本將 ^ 符號視作特殊字元,用於指定一行或者一個單詞的開始。例如輸出/etc/passes文件中所有以「root」開頭的行
root@Linux-world:~# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
root@Linux-world:~#
例7 使用 $ 符號輸出所有以指定模式結尾的行。
輸出/etc/passwd文件中所有以「bash」結尾的行。
root@Linux-world:~# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
Bash腳本將美元($)符號視作特殊字元,用於指定一行或者一個單詞的結尾。
例8 使用 -r 參數遞歸地查找特定模式
root@Linux-world:~# grep -r linuxtechi /etc/
/etc/subuid:linuxtechi:100000:65536
/etc/group:adm:x:4:syslog,linuxtechi
/etc/group:cdrom:x:24:linuxtechi
/etc/group:sudo:x:27:linuxtechi
/etc/group:dip:x:30:linuxtechi
/etc/group:plugdev:x:46:linuxtechi
/etc/group:lpadmin:x:115:linuxtechi
/etc/group:linuxtechi:x:1000:
/etc/group:sambashare:x:131:linuxtechi
/etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
............................................................................
上面的命令將會遞歸的在/etc目錄中查找「linuxtechi」單詞
例9 使用 grep 查找文件中所有的空行
root@Linux-world:~# grep ^$ /etc/shadow
root@Linux-world:~#
由於/etc/shadow文件中沒有空行,所以沒有任何輸出
例10 使用 -i 參數查找模式
grep命令的-i參數在查找時忽略字元的大小寫。
我們來看一個例子,在paswd文件中查找「LinuxTechi」單詞。
nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd
linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash
nextstep4it@localhost:~$
例11 使用 -e 參數查找多個模式
例如,我想在一條grep命令中查找『linuxtechi』和『root』單詞,使用-e參數,我們可以查找多個模式。
root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
例12 使用 -f 用文件指定待查找的模式
首先,在當前目錄中創建一個搜索模式文件「grep_pattern」,我想文件中輸入的如下內容。
root@Linux-world:~# cat grep_pattern
^linuxtechi
root
false$
root@Linux-world:~#
現在,試試使用grep_pattern文件進行搜索
root@Linux-world:~# grep -f grep_pattern /etc/passwd

例13 使用 -c 參數計算模式匹配到的數量
繼續上面例子,我們在grep命令中使用-c命令計算匹配指定模式的數量
root@Linux-world:~# grep -c -f grep_pattern /etc/passwd
22
root@Linux-world:~#
例14 輸出匹配指定模式行的前或者後面N行
a)使用-B參數輸出匹配行的前4行
root@Linux-world:~# grep -B 4 "games" /etc/passwd

b)使用-A參數輸出匹配行的後4行
root@Linux-world:~# grep -A 4 "games" /etc/passwd

c)使用-C參數輸出匹配行的前後各4行
root@Linux-world:~# grep -C 4 "games" /etc/passwd

via: http://www.linuxtechi.com/linux-grep-command-with-14-different-examples/
作者:Pradeep Kumar 譯者:cvsher 校對:wxy
本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive
對這篇文章感覺如何?
太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
More in:Linux中國
捐贈 Let's Encrypt,共建安全的互聯網
隨著 Mozilla、蘋果和谷歌對沃通和 StartCom 這兩家 CA 公司處罰落定,很多使用這兩家 CA 所簽發證書的網站紛紛尋求新的證書籤發商。有一個非盈利組織可以為大家提供了免費、可靠和安全的 SSL 證書服務,這就是 Let's Encrypt 項目。現在,它需要您的幫助
Let's Encrypt 正式發布,已經保護 380 萬個域名
由於 Let's Encrypt 讓安裝 X.509 TLS 證書變得非常簡單,所以這個數量增長迅猛。
關於Linux防火牆iptables的面試問答
Nishita Agarwal是Tecmint的用戶,她將分享關於她剛剛經歷的一家公司(印度的一家私人公司Pune)的面試經驗。在面試中她被問及許多不同的問題,但她是iptables方面的專家,因此她想分享這些關於iptables的問題和相應的答案給那些以後可能會進行相關面試的人。 所有的問題和相應的答案都基於Nishita Agarwal的記憶並經過了重寫。 嗨,朋友!我叫Nishita Agarwal。我已經取得了理學學士學位,我的專業集中在UNIX和它的變種(BSD,Linux)。它們一直深深的吸引著我。我在存儲方面有1年多的經驗。我正在尋求職業上的變化,並將供職於印度的P
Lets Encrypt 已被所有主流瀏覽器所信任
旨在讓每個網站都能使用 HTTPS 加密的非贏利組織 Lets Encrypt 已經得了 IdenTrust的交叉簽名,這意味著其證書現在已經可以被所有主流的瀏覽器所信任。從這個裡程碑事件開始,訪問者訪問使用了Lets Encrypt 證書的網站不再需要特別配置就可以得到 HTTPS 安全保護了。 Lets Encrypt 的兩個中級證書 ...

















