从文件shell脚本中提取子字符串

时间:2013-01-15 19:38:36

标签: linux shell grep substring

  

可能重复:
  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

有什么建议吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

grep -oP '> \d+\.\d+\.\d+\.\d+\.\d+' capture.txt | cut -d. -f5