腳本幫助過濾掉 IP 位址

腳本幫助過濾掉 IP 位址

我有一個大文字文件,我想從中提取資料行,我想提取包含我可以指定的 IP 位址的每一行 (zzzz)

2015-02-26 00:00:00 Local3.Info x.x.x.x Feb 26 05:19:52  y.y.y.y 00:05:06:17 , C8:D7:19:61:D1:9B DHCP REQ: Valid IP->Valid IP
2015-02-26 00:00:00 Local3.Info x.x.x.x Feb 26 05:32:56  y.y.y.y 00:0D:8A:80 , 48:F8:B3:54:43:EB DHCP REQ: Valid IP->Valid IP
2015-02-26 00:00:00 Local5.Notice   x.x.x.x Feb 26 05:32:56  z.z.z.z BTS Sending CDR: 067,H,00:F0:3A:99,00:0D:8A:80,48:F8:B3:54:43:EB,z.z.z.z,10780,906
2015-02-26 00:00:00 Local3.Info x.x.x.x Feb 26 05:32:56  y.y.y.y 00:0D:8A:80 , 48:F8:B3:54:43:EB DHCP ACK: Valid IP->Valid IP: y.y.y.y
2015-02-26 00:00:00 Local5.Notice   x.x.x.x Feb 26 05:00:11  z.z.z.z AAA: Modulation Change to 16QAM recvd from 00:16:C4:ED

所以新文件中的輸出將如下所示

2015-02-26 00:00:00 Local5.Notice   x.x.x.x Feb 26 05:32:56  z.z.z.z BTS Sending CDR: 067,H,00:F0:3A:99,00:0D:8A:80,48:F8:B3:54:43:EB,z.z.z.z,10780,906
2015-02-26 00:00:00 Local5.Notice   x.x.x.x Feb 26 05:00:11  z.z.z.z AAA: Modulation Change to 16QAM recvd from 00:16:C4:ED

任何幫助將不勝感激!先致謝

答案1

你應該能夠使用grep

grep -F 'z.z.z.z' logfile > results

-F( )參數--fixed-strings阻止使用正規表示式語法(將匹配「任何字元」)來解釋句點分隔符,而是將句點分隔符號解釋為字面句點。

答案2

您可以使用 grep 或 awk。

grep 'z.z.z.z' your_file

awk 有更多選項

awk '/z.z.z.z/ {print}' your_file

但 awk 可以進行更多格式化並且有更多選項

https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-in-linux

http://www.grymoire.com/Unix/Awk.html

欲了解詳情

還有很多其他選項,perl 也可以工作...

答案3

有人錯過了嗎sed?幹得好:

sed -i.bak '/z\.z\.z\.z/!d' file.txt

原始檔案將備份為“file.txt.bak”,修改後的檔案將為“file.txt”。如果您不想備份原始檔案:

sed -i '/z\.z\.z\.z/!d' file.txt

如果您只想列印輸出而不是儲存它:

sed '/z\.z\.z\.z/!d' file.txt

答案4

Perl 替代方案:

$ perl -lane 'print if /z.z.z.z/'  < input.txt                                                                               
2015-02-26 00:00:00 Local5.Notice   x.x.x.x Feb 26 05:32:56  z.z.z.z BTS Sending CDR: 067,H,00:F0:3A:99,00:0D:8A:80,48:F8:B3:54:43:EB,z.z.z.z,10780,906
2015-02-26 00:00:00 Local5.Notice   x.x.x.x Feb 26 05:00:11  z.z.z.z AAA: Modulation Change to 16QAM recvd from 00:16:C4:ED

相關內容