请记住一场以前的一行中的比赛,仅一次将连续的行包含与SED匹配的情况下一次



sed魔术通缉:

我想知道如何保存/记住一条线(如标题部分(而不立即打印

并在以下行中的某个地方找到其他某些东西,

但是,保存/记忆的标头零件应仅打印一次,以下任何数量的以下其他匹配项下方的其他匹配项

,如果在记住标头线的下面的任何以下行中都没有其他东西的匹配,则根本不应打印它

因此,对于ifconfig

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
    inet 127.0.0.1 netmask 0xff000000
    inet6 ::1 prefixlen 128
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
    inet 127.94.0.2 netmask 0xff000000
    inet 127.94.0.1 netmask 0xff000000
    nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
XHC20: flags=0<> mtu 0
en3: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
    ether 68:5b:35:c1:b3:91
    inet6 fe80::8ef:5953:53b:7058%en3 prefixlen 64 secured scopeid 0x5
    inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
    inet6 2a02:810d:9c0:59bb:c0d:c8af:7e27:42f1 prefixlen 64 autoconf secured
    inet6 2a02:810d:9c0:59bb:643f:a2cb:ac5f:7c71 prefixlen 64 autoconf temporary
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect (1000baseT <full-duplex,flow-control,energy-efficient-ethernet>)
    status: active
en0: flags=8823<UP,BROADCAST,SMART,SIMPLEX,MULTICAST> mtu 1500
    ether 6c:40:08:9c:45:ce
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect (<unknown type>)
    status: inactive

使用ifconfig | gsed -n -E '/^[a-z0-9]*:/h; /tinet (addr:)?[0-9.a-fA-F:]*/{x;p;x;p}'

我得到了(这已经很棒,但不好(:

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    inet 127.0.0.1 netmask 0xff000000
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    inet 127.94.0.2 netmask 0xff000000
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    inet 127.94.0.1 netmask 0xff000000
en3: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255

,但结果我只想为所有IPS获取一次标题(没有混乱(

lo0:
    inet 127.0.0.1
    inet 127.94.0.2
    inet 127.94.0.1
en3:
    inet 192.168.0.2

您可以尝试一次。

Your_command | awk '
/^[a-zA-Z]+/{
  count=""
  val=$1
  next
}
val && match($0,/.*[0-9]+.[-0-9]+.[0-9]+.[0-9]+ +/){
  if(++count==1){
    print val
  }
  value=substr($0,RSTART,RLENGTH)
  sub(/ +$/,"",value)
  print value
  value=""
}
' 

,所以我想出了如何在昨晚做的:(

ifconfig | gsed -n -E '
/^[a-z0-9]*:/ {                     # for lines starting with this
    s/^([a-z0-9]*:).*/1/;h         # extract the start and put it into hold space
}
/tinet (addr:)?[0-9.a-fA-F:]*/ {   # for lines containing this
    s/^.*(tinet (addr:)?[0-9.a-fA-F:]*).*/1/    # extract it
    x                               # swap hold space and pattern space
    G                               # and then append the hold space (former pattern space) to it 
    s/^n//                         # replace leading n if former hold space was empty
    p                               # print the concatenated former hold space and modified pattern space
    s/^.*$//                        # empty the pattern space
    x                               # swap the now empty pattern space to hold space
}
'

或(或多或少(一线:

ifconfig | gsed -n -E '
/^[a-z0-9]*:/                   { s/^([a-z0-9]*:).*/1/;h };
/tinet (addr:)?[0-9.a-fA-F:]*/ { s/^.*(tinet (addr:)?[0-9.a-fA-F:]*).*/1/;x;G;s/^n//;p;s/^.*$//;x}'

给我希望

lo0:
    inet 127.0.0.1
    inet 127.94.0.2
    inet 127.94.0.1
en3:
    inet 192.168.0.2

我有两个建议:

  1. 清洁输入线时,有时更容易删除而不是选择。
  2. 在这种情况下,以持有空间的最终输出收集更容易。

例如:

parse.sed

/^[^ ]+/ {        # For lines starting with non-space
  x               # 
  /n/p           #  Did we collect any IP addresses?
  x               # /
  s/ .*//         # Clean up interface name
  h               # Overwrite hold-space
}
/inet / {
  s/ netmask.*//
  H               # Collect ip addresses in hold-space
}
$ {
  x               # Print the last interface if
  /n/p           # it contained IP addresses
}

这样运行:

ifconfig | gsed -nEf parse.sed

输出:

lo0:
    inet 127.0.0.1
    inet 127.94.0.2
    inet 127.94.0.1
en3:
    inet 192.168.0.2

sed用于做简单的 s/old/new,即全部,对于其他任何东西,只需使用尴尬:

$ ifconfig | awk '/^[[:alpha:]]/{hdr=$1 ORS} $1=="inet"{print hdr "    " $1, $2; hdr=""}'
lo0:
    inet 127.0.0.1
    inet 127.94.0.2
    inet 127.94.0.1
en3:
    inet 192.168.0.2

这可能对您有用(gnu sed(:

sed -En '/^S/h;s/^(s+inets+S+).*/1/;T;x;s/s.*//p;x;p' file

将标题线存储在保留空间中。

删除包含inet的行的IP地址。

如果替代是不成功的,请救助。否则,返回到保持空间,然后尝试删除标题线的末端并打印。

返回模式空间并打印原始的IP地址行。

n.b。标题只会打印一次,因为替换已经在编辑线后会失败。

最新更新