我有一个包含以下内容的文件:
/interface bridge
add comment="Bridge VLAN18" fast-forward=no mtu=1500 name=\
bridge-vlan18 protocol-mode=none
/interface ethernet
set [ find default-name=ether3 ] comment="Ether3" \
name=cobre
set [ find default-name=ether5 ] comment="Ether5" disabled=\
yes name=cobre2
/interface bridge port
add bridge=bridge-vlan18 interface=vlan18.bonding-ptp3
add bridge=bridge-vlan18 interface=vlan18.ether13
add bridge=bridge-vlan18 interface=vlan18.eoip-funes-vlan18
add bridge=bridge-vlan18 comment="VLAN18" \
interface=eoip-orono-vlan18 path-cost=20
add bridge=bridge-vlan18 interface=vlan18.bonding-ptp5
/ip firewall connection tracking
set tcp-established-timeout=10m我需要提取从"/“到”next "/“的每一个文本块,而不需要第二个斜杠。我尝试了以下方法(提取“接口桥”块):
sed -n "/^\/interface\ bridge\s$/,/^\//p" file.txt但我明白:
/interface bridge
add comment="Bridge VLAN18" fast-forward=no mtu=1500 name=\
bridge-vlan18 protocol-mode=none
/interface ethernet我要得到:
/interface bridge
add comment="Bridge VLAN18" fast-forward=no mtu=1500 name=\
bridge-vlan18 protocol-mode=none它需要使用本地linux工具(grep、sed、awk等)。有什么我能做的建议吗?
发布于 2018-10-17 16:07:59
另一个awk
$ awk 'f && /^\//{exit} /^\/interface bridge\r?$/{f=1} f' file
/interface bridge
add comment="Bridge VLAN18" fast-forward=no mtu=1500 name=\
bridge-vlan18 protocol-mode=none这应该是使用/interface bridge令牌开始打印,然后退出如果处于打印模式,然后看到下一个/
发布于 2018-10-17 15:54:36
这被称为记录,因此在awk中,这提供了:
awk 'BEGIN{RS="/";ORS=""}/^interface bridge/{print RS $0}' file在这里,我们将内置变量RS定义为。RS是记录分隔符。输出记录分隔符ORS被设置为空字符串,因为每个记录已经以一个字符结束。
上面的语句是这样的:如果记录以interface bridge__开头,则使用它前面的记录分隔符RS打印记录。
但这将匹配以字符串“接口桥”开头的任何记录,因此也将匹配“接口桥端口”。更清洁一点的是:
awk 'BEGIN{RS="/";ORS=""; FS=" *\n *"}
($1=="interface bridge"){print RS $0}' file在这里,我们还在字段中拆分记录,这些字段是记录中的行。上面的语句读取,如果记录的第一个字段等于interface bridge__,则使用它前面的记录分隔符RS打印记录。
发布于 2018-10-17 17:59:26
用剪裁:
cut -z -d '/' -f -2 file.txthttps://stackoverflow.com/questions/52858963
复制相似问题