嗨,我有这个文本文件。
Physical interface: ge-0/0/3, Enabled, Physical link is Up
Interface index: 132, SNMP ifIndex: 504
Description: # SURVEILLANCE CAMERA #
Link-level type: Flexible-Ethernet, Media type: Copper, MTU: 9000,
LAN-PHY mode, Link-mode: Full-duplex, Speed: 1000mbps, BPDU Error: None,
.....few more lines
Physical interface: ge-0/1/0, Enabled, Physical link is Down
Interface index: 133, SNMP ifIndex: 505
Link-level type: Ethernet, Media type: Fiber, MTU: 1514, LAN-PHY mode,
Speed: 1000mbps, BPDU Error: None, MAC-REWRITE Error: None,
.....few more lines
Physical interface: ge-0/1/3, Enabled, Physical link is Up
Interface index: 136, SNMP ifIndex: 508
Description: # TO CSS_I-TN-CHNN-ENB-I099 #
Link-level type: Flexible-Ethernet, Media type: Fiber, MTU: 8000,
LAN-PHY mode, Speed: 1000mbps, BPDU Error: None, MAC-REWRITE Error: None,
... few more lines
and so on....
现在,如果“物理链接”已打开且MTU的值为9000,则只需要替换两条相应的行即可。
<Pass>Physical interface: ge-0/0/3, Enabled, Physical link is Up
&
<Pass>Link-level type: Flexible-Ethernet, Media type: Fiber, MTU: 9000,
在任何其他情况下它将<Fail>
代替<Pass>
。这些值位于不同的行中,这就是为什么我不了解使用sed或其他任何东西的原因..请帮助...这是预期的输出..
<Pass>Physical interface: ge-0/0/3, Enabled, Physical link is Up
Interface index: 132, SNMP ifIndex: 504
Description: # SURVEILLANCE CAMERA #
<Pass>Link-level type: Flexible-Ethernet, Media type: Copper, MTU: 9000,
LAN-PHY mode, Link-mode: Full-duplex, Speed: 1000mbps, BPDU Error: None,
.....few more lines
<Fail>Physical interface: ge-0/1/0, Enabled, Physical link is Down
Interface index: 133, SNMP ifIndex: 505
<Fail>Link-level type: Ethernet, Media type: Fiber, MTU: 1514, LAN-PHY mode,
Speed: 1000mbps, BPDU Error: None, MAC-REWRITE Error: None,
.....few more lines
<Fail>Physical interface: ge-0/1/3, Enabled, Physical link is Up
Interface index: 136, SNMP ifIndex: 508
Description: # TO CSS_I-TN-CHNN-ENB-I099 #
<Fail>Link-level type: Flexible-Ethernet, Media type: Fiber, MTU: 8000,
LAN-PHY mode, Speed: 1000mbps, BPDU Error: None, MAC-REWRITE Error: None,
... few more lines
and so on....
与sed:
sed '/Physical link is/ { :a /MTU:/! { N; ba; }; /Physical link is Up.*MTU: 9000/ { s/\(.*\n\)\s*/<Pass>\1<Pass>/; b; }; s/\(.*\n\)\s*/<Fail>\1<Fail>/; }' filename
那是:
/Physical link is/ { # Block start found
:a
/MTU:/! { # fetch lines until we find the MTU
N
ba
}
/Physical link is Up.*MTU: 9000/ { # If link is up and MTU 9000
s/\(.*\n\)[[:space:]]*/<Pass>\1<Pass>/ # insert Pass markers
b
# we're done.
}
s/\(.*\n\)[[:space:]]*/<Fail>\1<Fail>/ # otherwise insert Fail markers
}
请注意,对于BSD sed,由于b
说明,它不能用作单线。在这种情况下,将扩展的代码(不带注释,因为BSD sed容易混淆)放在文件中foo.sed
,然后使用sed -f foo.sed filename
。我已经\s
用它的POSIX等效项([[:space:]]
)替换了其他GNU-ism()。
要将空格保留在MTU行的开头,请删除\s
或[[:space:]]
。要将空格放在结果标记之前,请将\s
或[[:space:]]
放在捕获组内(即\(.*\n\s*\)
)。
另请注意:这假设每个接口描述都有一个MTU字段。
或者,您可以尝试以下awk脚本:
awk -v RS='Physical interface:' -F '\n' -v OFS='\n' '{ result = "<Fail>" } /Physical link is Up/ && /MTU: 9000/ { result = "<Pass>" } NR != 1 { for(i = 1; i <= NF; ++i) { if(index($i, "MTU:")) { sub(/^ */, result, $i) } } print result RS $0 }' filename
这会将文件拆分为的记录,Physical interface:
并将记录拆分为换行的字段。然后:
{ result = "<Fail>" } # result is Fail
/Physical link is Up/ && /MTU: 9000/ { # unless link is up and MTU 9000
result = "<Pass>"
}
NR != 1 { # the first record is the empty string
# before the first actual record, so
# we remove it.
for(i = 1; i <= NF; ++i) { # wade through the fields (lines)
if(index($i, "MTU:")) { # find the MTU line
sub(/^ */, result, $i) # put the marker there. To keep the
# whitespace, use $i = result $i
# instead, or sub(/^ */, "&" result, $i)
# to keep the spaces before the marker.
}
}
print result RS $0 # once done, print the whole shebang.
# We have to reinsert the record
# separator because it was removed
# by the splitting.
}
请注意,多字符RS
并不严格符合POSIX。不过,最常见的awks(gawk和mawk)都支持它。值得注意的是,BSD awk没有。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句