首页 > 解决方案 > 正则表达式在没有特定模式时选择特定单词?

问题描述

我有一个这种格式的数据列表:

eth0: flags=73<UP,LOOPBACK,RUNNING>  mtu 1500
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0xfe<compat,link,site,host>
    loop  (Local Loopback)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=73<UP,LOOPBACK,RUNNING>  mtu 1500
    inet6 ::1  prefixlen 128  scopeid 0xfe<compat,link,site,host>
    loop  (Local Loopback)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我需要选择eth1(这是第一个单词,它是一个总是以 开头的单词e)它后面没有127.0.0.1(它也可能出现在下一行的后面)。

这里eth0是不合格的,因为它后面是127.0.0.1

我尝试了一切,但似乎没有任何效果。甚至可以使用正则表达式吗?如果是,那怎么办?

标签: regexubuntuawk

解决方案


$ awk -v RS= -F':' '!/[[:space:]]127\.0\.0\.1[[:space:]]/{print $1}' file
eth1

这将在每个 UNIX 机器上的任何 shell 中使用任何 POSIX awk 工作。


推荐阅读