grep 同時排除多個關鍵字

2021-08-07 09:44:30 字數 1385 閱讀 8027

不說廢話, 例如需要排除 abc.txt 中的  mmm   nnn

grep -v '

mmm\|nnn

' abc.txt

再舉個例子,需要確定mac 的本機ip位址,  顯然直接可以輸入 ifconfig, 但是會出來一大堆資訊,那麼再通過 grep inet 可以拿到類似如下的資訊:

bash-3.2# ifconfig | grep inet

inet 127.0.0.1 netmask 0xff000000

inet6 ::

1 prefixlen 128

inet6 fe80::

1%lo0 prefixlen 64 scopeid 0x1

inet6 fe80::c37:dee4:5ad4:944b%en0 prefixlen 64 secured scopeid 0x4

inet

10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255

inet6 fe80::8dc:a3ff:feaf:fbe1%awdl0 prefixlen 64 scopeid 0x9

inet6 fe80::bd0c:

5502:92ad:25e1%utun0 prefixlen 64 scopeid 0xa

但是這樣還是很多,需要從這幾條資訊裡面去找到所需要的 ip 位址,我們可能想到了使用 grep -v 遮蔽掉 inet6,結果如下:

bash-3.2# ifconfig | grep inet | grep -v inet6

inet

127.0.0.1 netmask 0xff000000

inet

10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255

這樣其實也可以看了,但是強迫症患者傷不起啊,我就只要一條,怎麼弄呢,顯然還可以繼續通過 grep -v 127.0.0.1 來遮蔽掉第一條記錄,如下:

bash-3.2# ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1

inet

10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255

這樣好像不是很優雅,那就剛剛說的方法,如下:

bash-3.2# ifconfig | grep inet | grep -v '

inet6\|127.0.0.1

'inet

10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255

grep 滿足 或 排除多個關鍵字

grep e word1 word2 word3 file.txt 滿足任意條件 word1 word2和word3之一 將匹配。grep word1 file.txt grep word2 grep word3 必須同時滿足三個條件 word1 word2和word3 才匹配。不說廢話,例如需要排...

grep 基於關鍵字搜尋

grep linux etc passwd 搜尋passwd檔案下的包含linux的行 find user linux grep video 在使用者為linux的根目錄下搜房video內容 netstat tnpl grep server 查詢網路程序為server的資料 i在搜尋的時候忽略大小寫...

如何優化多個關鍵字

比如像點石會員zeroth在這個帖子裡問到的 雲南旅遊這個詞很大,下面好的詞還有,昆明旅遊,麗江旅遊,大理旅遊,騰衝旅遊,西雙版納旅遊,香格里拉旅遊,臨滄旅遊,這些好的詞,我該怎樣把他們聯絡起來呢?他們都屬於雲南旅遊。第個旅遊 都會有這些版塊,但我覺得他們幾乎沒什麼聯絡,所以根本出不來效果。怎樣才能...