linux - 从文件中提取 shell 脚本中一行的子字符串

标签 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 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的端口。

最佳答案

使用 lookarounds使用 grep:

$ 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

关于linux - 从文件中提取 shell 脚本中一行的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14381557/

相关文章:

grep 由两个关键行分隔的文本 block

linux - 使用模式和输入文件进行 grep 过滤

linux - 厌倦了在每次重启后创建/运行/postgresql 并设置读取和执行写入

linux - 获取包含文件夹的名称

java - JavaFX 运行时 JAR 文件 jfxrt.jar 在 Linux 上的位置是什么?

linux - xargs 可以用于并行运行多个任意命令吗?

bash - 如何合并两个不包含重复项的csv文件

linux - 为什么 open 调用需要两个参数(struct inode *、struct file*)?

Bash getopts : recognizing negative options (-x- or +x)?

c - 这个基本的 shell 程序有什么问题?前几个命令运行良好,但结果总是以段错误结束