我有以下输出,我想替换 IP 地址后面以“/”开头的所有内容,直到但不包括单词 vlan 的第一个实例。如何才能做到这一点呢?
sy3-fa-c3r801-lb-3b.audc1.xxxxx.com address 192.168.1.2/30 allow-service { default traffic-group Common/traffic-group-local-only vlan Common/vlan-HA }
sy3-ibr-lb-1a.audc1.xxxxx.com address 160.34.63.68/29 floating enabled traffic-group Common/traffic-group-1 unit 1 vlan Common/vlan13
sy3-ibr-lb-1a.audc1.xxxxx.com address 160.34.63.193/26 floating enabled traffic-group Common/traffic-group-1 unit 1 vlan Common/vlan23
sy3-ibr-lb-1a.audc1.xxxxx.com address 192.168.1.1/30 allow-service { default traffic-group Common/traffic-group-local-only vlan Common/vlan-HA }
sy3-ibr-lb-1a.audc1.xxxxx.com address 160.34.63.194/26 traffic-group Common/traffic-group-local-only vlan Common/vlan23
sy3-ibr-lb-1a.audc1.xxxxx.com address 160.34.63.69/29 traffic-group Common/traffic-group-local-only vlan Common/vlan13
sy3-ibr-lb-1b.audc1.xxxxx.com address 160.34.63.68/29 floating enabled traffic-group Common/traffic-group-1 unit 1 vlan Common/vlan13
sy3-ibr-lb-1b.audc1.xxxxx.com address 160.34.63.193/26 floating enabled traffic-group Common/traffic-group-1 unit 1 vlan Common/vlan23
sy3-ibr-lb-1b.audc1.xxxxx.com address 192.168.1.2/30 allow-service { default traffic-group Common/traffic-group-local-only vlan Common/vlan-HA }
sy3-ibr-lb-1b.audc1.xxxxx.com address 160.34.63.195/26 traffic-group Common/traffic-group-local-only vlan Common/vlan23
sy3-ibr-lb-1b.audc1.xxxxx.com address 160.34.63.70/29 traffic-group Common/traffic-group-local-only vlan Common/vlan13
答案1
sed 's/\(.*[0-9]\)\/.* \(vlan.*\)/\1REPLACEMENT\2/' file
通过其前面的数字匹配第一次出现的和通过其空格字符匹配/
第一次出现的。vlan
答案2
也可能有效
sed -r 's/vlan/\x01/;s/(([0-9]{1,3}[.]?){4})\/.*\x01/\1...vlan/' file
使用第一个占位符vlan
来避免sed
贪婪。是的,IP 中的“可选”点存在一个小漏洞。如果需要,请使用更复杂的正则表达式。