我有一个这样的文件:
device1;interface Gi0/1
device1;interface Gi0/2
device1;interface Gi0/3
device1; description Hello
device1;interface Gi0/4
device2;interface Gi0/1
device2; ip vrf forwarding xxx-vrf
device2;interface Gi0/2
device2;interface Gi0/3
device2;interface Gi0/4
device2; ip address 192.168.1.1 255.255.255.254
我希望这会返回每个没有其他配置的接口,这些接口在下一行没有以'interface'开头(见上文) - 即。在这种情况下,我想要:
device1;interface Gi0/1
device1;interface Gi0/2
device1;interface Gi0/4
device1;interface Gi0/2
device2;interface Gi0/3
我发现这很棘手,我不知道如何去做。为我的业余道歉。
第
答案 0 :(得分:1)
尝试:
awk '/interface/{if(p)print p; p=$0; next}{p=x} END{if(p)print p}' file
以下是解释:
awk '
/interface/ { # if a line contains "interface"
if(p"")print p # then if variable p is non-empty (and if the previous line contained
# "interface"), then print its content.
p=$0 # store the current line in variable "p" (if it contains "interface)
next # read the next line
}
{ # if the line does not contain "interface"
p=x # empty variable p by assigning an empty variable to it
}
END { # After all lines have been processed
if(p"")print p # then if variable p still contains something, print it.
}
' file
编辑:如果条件强制将其加入字符串上下文,则在p in中添加双引号。
答案 1 :(得分:0)
另一个awk
awk 'a=/interface/{if(b)print b;b=$0}!a{b=""}END{print b}' file
制作
device1;interface Gi0/1
device1;interface Gi0/2
device1;interface Gi0/4
device2;interface Gi0/2
device2;interface Gi0/3