Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 从文件中,从shell脚本中的行中提取子字符串_Linux_Shell_Grep_Substring - Fatal编程技术网

Linux 从文件中,从shell脚本中的行中提取子字符串

Linux 从文件中,从shell脚本中的行中提取子字符串,linux,shell,grep,substring,Linux,Shell,Grep,Substring,我需要帮助从文件中获取子字符串。我有两个变量,IP源地址和IP目标地址。我需要验证文件中包含两个IP的行,并获取源地址的端口 这是输入文件: 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 0

我需要帮助从文件中获取子字符串。我有两个变量,IP源地址和IP目标地址。我需要验证文件中包含两个IP的行,并获取源地址的端口

这是输入文件:

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       ..............
两个变量:

ips=10.0.0.155

ipd=239.255.255.254
输出结果必须是:

58363   
这是IP源地址
10.0.0.155.58363

的端口,与
grep
一起使用:

$ ips=10.0.0.155

$ ipd=239.255.255.254

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

希望对你有帮助。可以用变量替换IP

[spatel@mg0008 ~]$ 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
更多的修剪

[spatel@mg0008 tmp]$ grep 10.0.0.155.58363 /tmp/outputfile.txt | awk -F'.' '{print $5}' | awk '{print $1}'
58363
58363

这个输出结果是什么?没有任何意义..是ip地址源的端口10.0.0.155.58363这不会检查目标ip的行,因此会有错误匹配,端口未知,并且您已将其硬编码到搜索中,也不必使用
grep | awk | awk
。这不是给定问题的解决方案。
[spatel@mg0008 tmp]$ grep 10.0.0.155.58363 /tmp/outputfile.txt | awk -F'.' '{print $5}' | awk '{print $1}'
58363
58363