代码之家  ›  专栏  ›  技术社区  ›  kikilinux

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

  •  0
  • kikilinux  · 技术社区  · 9 年前

    使用命令行工具,我需要在关键字“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
    
    2 回复  |  直到 9 年前
        1
  •  0
  •   fedorqui    9 年前

    假设您只希望这种情况发生一次,例如,您可以说:

    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 
    
        2
  •  0
  •   Michael Vehrs    9 年前

    你也可以使用

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