2013-01-17 30 views
0

我需要幫助才能從文件中獲取子字符串。我有兩個變量,IP源和IP目標地址。我需要驗證包含兩個IP的文件中的行並獲取源地址的端口。從文件中提取shell腳本中一行的子字符串

這是輸入文件:

15:29:18.164566 IP (tos 0x0, ttl 1, id 2394, offset 0, flags [none], proto UDP (17), length 125) 
    10.0.0.155.58363 > 239.255.255.254.1900: UDP, length 97 
    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  .............. 
15:29:18.164566 IP (tos 0x0, ttl 128, id 2394, offset 0, flags [none], proto UDP (17), length 125) 
    10.0.0.131.58363 > 239.255.255.250.1900: UDP, length 97 
    0x0000: 4600 0024 0000 0000 0102 3ad3 0a00 0000 F..$......:..... 
    0x0010: e000 0001 9404 0000 1101 ebfe 0000 0000 ................ 
15:29:18.164566 IP (tos 0x0, ttl 1, id 2394, offset 0, flags [none], proto UDP (17), length 125) 
    10.0.0.155.58363 > 239.255.255.254.1900: UDP, length 97 
    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  .............. 
15:29:18.164566 IP (tos 0x0, ttl 128, id 2394, offset 0, flags [none], proto UDP (17), length 125) 
    10.0.0.131.58363 > 239.255.255.250.1900: UDP, length 97 
    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  .............. 
    0x0020: 0300 0000 0000 0000 0000 0000 0000  .............. 
15:29:18.164566 IP (tos 0x0, ttl 128, id 2394, offset 0, flags [none], proto UDP (17), length 125) 
    10.0.0.155.80 > 239.255.255.250.1900: UDP, length 97 
    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  .............. 
    0x0020: 0300 0000 0000 0000 0000 0000 0000  .............. 

兩個VARS:

ips=10.0.0.155 

ipd=239.255.255.254 

輸出結果必須是:

58363 

這是IP源地址的端口10.0.0.155.58363

+0

這是什麼輸出結果?沒有任何意義.. – Satish

+0

是ip adreess源端口10.0.0.155.58363 – Litox

回答

1

使用lookaroundsgrep

$ ips=10.0.0.155 

$ ipd=239.255.255.254 

$ grep -Po "(?<=$ips\.)\d+(?= > $ipd)" file 
58363 
58363 

文件中有重複行,以便管uniq

$ grep -Po "(?<=$ips\.)\d+(?= > $ipd)" file | uniq 
58363 

或使用帶有sed捕獲組:

$ sed -n '/'"$ipd"'/s/.*'"$ips"'\.\([0-9]\+\).*/\1/p' file 
58363 
58363 

$ sed -n '/'"$ipd"'/s/.*'"$ips"'\.\([0-9]\+\).*/\1/p' file | uniq 
58363 

或者在awk

$ awk -v s=$ips -v d=$ipd '$1~s && $3~d {sub(/.*\./,"",$1);print $1}' file 
58363 
58363 

$ awk -v s=$ips -v d=$ipd '$1~s&&$3~d&&!u[$0]++{sub(/.*\./,"",$1);print $1}' file 
58363 
0

希望它可以幫助你。你可以用你的變量替換IP。

[[email protected] ~]$ grep 10.0.0.155.58363 /tmp/outputfile.txt 
    10.0.0.155.58363 > 239.255.255.254.1900: UDP, length 97 
    10.0.0.155.58363 > 239.255.255.254.1900: UDP, length 97 

更多微調...

[[email protected] tmp]$ grep 10.0.0.155.58363 /tmp/outputfile.txt | awk -F'.' '{print $5}' | awk '{print $1}' 
58363 
58363 
+0

這不檢查目標IP的行,所以將有錯誤的匹配和端口是未知的,你已經硬編碼到也搜索'grep | awk | awk'沒有必要。這不是解決問題的辦法。 –