【问题标题】:Extract substring from a file shell script [duplicate]从文件shell脚本中提取子字符串[重复]
【发布时间】:2013-01-15 19:38:36
【问题描述】:

可能重复:
i need get a substring from a file shell script

我需要一点帮助。我只有一个包含命令捕获的文件。

tcpdump -Xvv -n proto \tcp -c 10 > capture.txt 2>/dev/null

输出 capture.txt 的结果是这样的:

15:29:18.164566 IP (tos 0x0, ttl 1, id 2394, offset 0, flags [none], proto TCP (6), length 125)
    10.0.0.243.61908 > 10.0.0.184.80: Flags [S], cksum 0x3d53 (correct), seq 1831050442, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
    0x0000:  4600 0024 0000 0000 0102 3ad3 0a00 0000  F..$......:.....
    0x0010:  e000 0001 9404 0000 1101 ebfe 0000 0000  ................
   ..............
15:29:18.164567 IP (tos 0x0, ttl 128, id 2394, offset 0, flags [none], proto TCP (6), length 125)
    10.0.0.184.80 > 10.0.0.243.61908: Flags [S.], cksum 0x135e (correct), seq 2906424792, ack 1831050443, win 5840, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
    0x0000:  4600 0024 0000 0000 0102 3ad3 0a00 0000  F..$......:.....
    0x0010:  e000 0001 9404 0000 1101 ebfe 0000 0000  ................
    0x0020:  0000 0000 0000 0000 2323 2332 2323
    0x0030:  0300 0000 0000 0000 0000 0000 0000       ..............
15:29:18.164569 IP (tos 0x0, ttl 1, id 2394, offset 0, flags [none], proto TCP (6), length 125)
    10.0.0.243.61908 > 10.0.0.184.80: Flags [S], cksum 0x3d53 (correct), seq 1831050442, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
    0x0000:  4600 0024 0000 0000 0102 3ad3 0a00 0000  F..$......:.....
    0x0010:  e000 0001 9404 0000 1101 ebfe 0000 0000  ................
    0x0020:  0300 0000 0000 0000 0000 0000 0000       ..............

我正在尝试使用 grep 命令:grep -A 但我无法使其工作。

我需要将目标端口定位在 ip 目标地址之后。

输出必须是:

80

61908

80

有什么建议吗?

【问题讨论】:

  • 你的grep 命令是什么样子的?
  • grep -A 1 -e 'proto TCP' capture.txt | grep '^' | ...这里我正在尝试使用cut,但我找不到正确的命令

标签: linux shell grep substring


【解决方案1】:

这似乎在这里工作。

tcpdump -Xvv -i wlan1 -n -nn proto \TCP -c 10 > capture.txt

sed -n 's/.*\.\(.*\): Flags.*/\1/p' capture.txt
443
35673
443
35071
80

【讨论】:

  • 我试过了,但这里出了点问题。这是输出 22 62036 80 Date ], seq 1 ], seq 1461 ], seq 3472502811
  • 完美。非常感谢!
【解决方案2】:
grep -oP '> \d+\.\d+\.\d+\.\d+\.\d+' capture.txt | cut -d. -f5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2021-02-12
    相关资源
    最近更新 更多