如何在Linux中提取文件的特定行和列?
使用命令行工具,我需要提取文本“HTTP”,(或任何协议恰好是),关键字“检测的协议”之后:如何在Linux中提取文件的特定行和列?
部分文件内容:
Detected protocol:
HTTP 1254
完整的文件内容:
nDPI Memory statistics:
nDPI Memory (once): 103.29 KB
Flow Memory (per flow): 1.91 KB
Actual Memory: 1.76 MB
Peak Memory: 1.76 MB
Traffic statistics:
Ethernet bytes: 1342 (includes ethernet CRC/IFC/trailer)
Discarded bytes: 0
IP packets: 10 of 10 packets total
IP bytes: 1102 (avg pkt size 110 bytes)
Detected protocols:
HTTP packets: 10 bytes: 1102 flows: 1
Protocol statistics:
Acceptable 1102 bytes
假设你只是希望这一次发生的,比如,你可以说:
awk 'matched {print $2; exit} /Detected protocol/ {matched=1}' file
它返回:
1254
此检查是一个行包含 “检测到协议”。如果是这样,它会设置一个标志,在下一行中提示打印。然后,它退出。
您还可以在字段分隔符设置为HTTP
:
awk -F"HTTP" 'f {print $2; exit} /Detected protocol/ {f=1}' file
它返回更新后的输入文件如下:
packets: 10 bytes: 1102 flows: 1
发生了以下错误:awk:cmd。行:1:匹配{print $ 2;退出} /检测到的协议/ {匹配= 1} awk:cmd 。行:1:^语法错误 awk:cmd。行:1:匹配{print $ 2;退出} /检测到的协议/ {匹配= 1} awk:cmd 。行:1:^语法错误 – user3728748
@ user3728748这是我的错,因为我使用'match'作为变量名,而它是一个保留字。将其更改为“awk”匹配{print $ 2;退出} /检测到的协议/ {匹配= 1}'文件'。 – fedorqui
你也可以使用
sed -n '/Dectected protocol/{n;p}' $file
我会建议使用多行sed' - 见[这里](http://superuser.com/a/634848/438726),或者如果你需要更多的控制,'awk'。 – urban
您刚刚给出的此示例输入的预期输出是什么?尝试阅读[问]在这里有更好的体验。 – fedorqui