xxinganling 发表于 2022-04-28 16:17

如何在分组文本中提取固定和不固定位置信息?

有文本部分内容如下:
$ cat conf.txt
....

#
interface LoopBack600
description to-lb600
ip address 192.168.2.45 255.255.255.255
#
interface LoopBack800
to-lb800
ip address 172.18.32.96 255.255.255.255
#
#
interface Vlan-interface634
description TO-unit34
ip address 192.168.1.66 255.255.255.252
ospf cost 200
ospf network-type p2p
ospf bfd enable
#
#
interface FastEthernet 0/30
no switchport
ip ospf network point-to-point
ip ospf authentication message-digest
ip ospf message-digest-key 1 md5 7 141a46076330240c541752
ip ospf cost 50
no ip proxy-arp
ip address 192.168.6.32 255.255.255.252
description Connect_to_unit30
#
....

要从文本中提取含有“ip address”的行所在的一组文本(以#号区隔)的字头(FastEthernet 0/30等)、IP地址(ip address)和描述信息(description),输出以逗号区隔的如下文本:
LoopBack600,192.168.2.45,to-lb600
LoopBack800,172.18.32.96,to-lb800
Vlan-interface634,192.168.1.66,TO-unit34
FastEthernet 0/30,192.168.6.32,Connect_to_unit30
...
研究了好长时间,因为每组数的行数不固定,要取得信息除了字头外其他两项所在行的位置也不固定,没能实现。请论坛里的高手帮忙,谢谢!




a5love3n 发表于 2022-04-29 11:28

本帖最后由 a5love3n 于 2022-04-29 11:39 编辑

awk '/interface/{if(b){print a","b","c;b=c=""};a=$2" "$3}/ip address/{b=$3}/description/{c=$2}END{print a","b","c}' 1.txtto-lb800这一行缺少 description,我才是你复制漏了,如果不是,那就不好处理了,因为description信息是管理员自定义的,没有特征

效果如下

LoopBack600 ,192.168.2.45,to-lb600
LoopBack800 ,172.18.32.96,to-lb800
Vlan-interface634 ,192.168.1.66,TO-unit34
FastEthernet 0/30,192.168.6.32,Connect_to_unit30

因为 interface FastEthernet 0/30 接口和其他接口格式不一样,为了兼顾,会导致 LoopBack600 ,这里的逗号前面多一个空格,介意的话,再用sed或者vim处理一下就行了

xxinganling 发表于 2022-04-29 14:22

回复 2# a5love3n

谢谢a5love3n !!!
脚本完全合适!我以为要重置FS为#后再配置各行,但没做成。您的这种连续匹配的方法我还是头一回见到,有对应的术语或相关的资料推荐吗?再次感谢 a5love3n !!!


另外,确实是丢了 description 字段。完整文件如下:
#
interface LoopBack600
description to-lb600
ip address 192.168.2.45 255.255.255.255
#
interface LoopBack800
description to-lb800
ip address 172.18.32.96 255.255.255.255
#
#
interface Vlan-interface634
description TO-unit34
ip address 192.168.1.66 255.255.255.252
ospf cost 200
ospf network-type p2p
ospf bfd enable
#
#
interface FastEthernet 0/30
no switchport
ip ospf network point-to-point
ip ospf authentication message-digest
ip ospf message-digest-key 1 md5 7 141a46076330240c541752
ip ospf cost 50
no ip proxy-arp
ip address 192.168.6.32 255.255.255.252
description Connect_to_unit30
#


a5love3n 发表于 2022-04-30 09:53

回复 3# xxinganling

专业术语和资料我还真不知道,我是个半桶水,我的awk就是在这里刷帖子学的,主要学习对象就是版主 wh7211 对各种问题的解决

本友会机友会摄友会 发表于 2022-05-02 11:37

wh7211 发表于 2022-06-07 17:20

回复 1# xxinganling
回复 2# a5love3n


这样定义变量a,逗号前面就没有空格了。
cat 1
#
interface LoopBack600
description to-lb600
ip address 192.168.2.45 255.255.255.255
#
interface LoopBack800
description to-lb800
ip address 172.18.32.96 255.255.255.255
#
#
interface Vlan-interface634
description TO-unit34
ip address 192.168.1.66 255.255.255.252
ospf cost 200
ospf network-type p2p
ospf bfd enable
#
#
interface FastEthernet 0/30
no switchport
ip ospf network point-to-point
ip ospf authentication message-digest
ip ospf message-digest-key 1 md5 7 141a46076330240c541752
ip ospf cost 50
no ip proxy-arp
ip address 192.168.6.32 255.255.255.252
description Connect_to_unit30
#

awk '/^interface/{if(a&&b&&c){print a","b","c};a=b=c="";a=gensub($1" ","","1",$0)}/^ip address/{b=$3}/^description/{c=$2}END{if(a&&b&&c){print a","b","c}}' 1
LoopBack600,192.168.2.45,to-lb600
LoopBack800,172.18.32.96,to-lb800
Vlan-interface634,192.168.1.66,TO-unit34
FastEthernet 0/30,192.168.6.32,Connect_to_unit30
页: [1]
查看完整版本: 如何在分组文本中提取固定和不固定位置信息?