【问题标题】:Substring from a header shell script来自标头 shell 脚本的子字符串
【发布时间】:2013-01-10 15:55:46
【问题描述】:

我需要帮助,我有来自 tcpdump 标头的下一行:

11:46:24.851239 IP (tos 0x0, ttl 128, id 11289, offset 0, flags [none], proto UDP (17), length 229)

我需要指定一个 header 的值(例如:proto),结果必须是:

proto UDP (17)

我正在尝试使用那个 shell 脚本程序:

var=flags
echo '11:46:24.851239 IP (tos 0x0, ttl 128, id 11289, offset 0, flags [none], proto UDP (17), length 229)' | grep -oP "\b$var+"

【问题讨论】:

    标签: linux shell unix grep


    【解决方案1】:

    一种方式:

    echo '11:46:24.851239 IP (tos 0x0, ttl 128, id 11289, offset 0, flags [none], proto UDP (17), length 229)' | grep -o 'proto [^,]\+'
    

    使用变量:

    $ x="proto"
    $ echo '11:46:24.85123........., proto UDP (17), length 229)' | grep -o "${x} [^,]\+"
    proto UDP (17)
    

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 1970-01-01
      • 2022-01-23
      • 2012-12-21
      • 1970-01-01
      • 2020-07-28
      • 2016-04-12
      • 2013-09-25
      • 1970-01-01
      相关资源
      最近更新 更多