我有一个很大的文本文件,我想从中提取数据行,我想提取包含我可以指定的 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 可以进行更多格式化,并且有更多选项
和
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