我试图弄清楚如何打印两个字符串之间发生的所有内容。问题是,这些字符串对在一行中出现多次。所以我需要能够打印每组对中的每个字段。
我有一个文件api.txt
列出了多个客户及其各自的设备清单。它看起来像这样:
Customer [customerId=12000, customerName=Acme, Inc.]
DeviceDetail [baseProductId=router-100, cardDetail=[CardDetail [baseCardId=router-100NIC1, cardDescription=Router 100 NIC, cardSerial=100NIC1], CardDetail [baseCardId=router-100NIC2, cardDescription=Router 100 NIC, cardSerial=100NIC2]], deviceSerial=100PRIMARY, deviceDescription=Router 100 Base Model]
DeviceDetail [baseProductId=router-2500, cardDetail=[CardDetail [baseCardId=router-2500NIC1, cardDescription=Router 2500 NIC, cardSerial=2500NIC1], CardDetail [baseCardId=router-2500NIC2, cardDescription=Router 2500 NIC, cardSerial=2500NIC2]], deviceSerial=2500PRIMARY, deviceDescription=Router 2500 Base Model]
Customer [customerId=24000, customerName=Anvil LLC]
DeviceDetail [baseProductId=router-5000, cardDetail=[CardDetail [baseCardId=router-5000NIC1, cardDescription=Router 5000 NIC, cardSerial=5000NIC1], CardDetail [baseCardId=router-500NIC2, cardDescription=Router 5000 NIC, cardSerial=5000NIC2]], deviceSerial=5000PRIMARY, deviceDescription=Router 5000 Base Model]
DeviceDetail [baseProductId=router-7500, cardDetail=null, deviceSerial=7500PRIMARY, DeviceDescription=Router 7500 Base Model, No NIC]
其输出应类似于:
"12000","Acme, Inc.","router-100","100PRIMARY","Router 100 Base Model","Router 100 NIC","100NIC1","Router 100 NIC","100NIC2"
"12000","Acme, Inc.","router-2500","2500Primary","Router 2500 Base Model","Router 2500 NIC","2500NIC1","Router 2500 NIC","2500NIC2"
"24000","Anvil LLC","router-5000","5000PRIMARY","Router 5000 Base Model,"Router 5000 NIC","5000NIC1","Router 5000 NIC","5000NIC2"
请注意,最后一个 DeviceDetail ( router-7500
) 被省略,因为该设备没有附加子设备 ( cardDetail=null
)。
我了解如何使用awk
将字段分隔符设置为=
并,
捕获它们之间的所有内容(即每个字段值位于等号和逗号之间),但我不确定如何在多个时获得我正在寻找的结果数据的实例CardDetail
在每一行上可能出现的次数未知,甚至根本不出现。
需要考虑的是,每个实例都Card Detail
被捕获在Card Detail
一个封闭的括号 ( ]
) 之间,因此这可能有助于捕获每Card Detail
行上的每个实例,但不确定。
我也没有结婚awk
。使用sed
或任何其他解析程序也可以。基本上,任何效果最好的。
提前感谢您提供的任何帮助!