如何在Linux中提取文件的特定行和列?

How to to extract a specific line and column of a file in Linux?

使用命令行工具,我需要在关键字 "Detected protocol":

之后提取文本 "HTTP"(或任何协议)

部分文件内容:

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 ; exit} /Detected protocol/ {matched=1}' file

哪个returns:

1254

此检查是一行包含 "Detected protocol"。如果是这样,它会设置一个标志,提示在下一行打印。然后,它退出。

您也可以将字段分隔符设置为HTTP:

awk -F"HTTP" 'f {print ; exit} /Detected protocol/ {f=1}' file

其中 returns 以下为您更新的输入文件:

                 packets: 10            bytes: 1102          flows: 1 

您也可以使用

sed -n '/Dectected protocol/{n;p}' $file