我正在处理这个文本文件:
Device ID: BIOTERIO
IP address: 148.000.00.189
Interface: GigabitEthernet1/0/6, Port ID (outgoing port): GigabitEthernet0/1
Device ID: N7K-LAN(JAF1651ANDL)
IP address: 148.000.0.192
Interface: GigabitEthernet1/0/1, Port ID (outgoing port): Ethernet7/23
Device ID: LAB_PESADO
IP address: 148.000.000.130
Interface: GigabitEthernet1/0/11, Port ID (outgoing port): GigabitEthernet0/1
IP address: 148.000.000.130
Device ID: Arquitectura_Salones
IP address: 148.000.000.61
Interface: GigabitEthernet1/0/9, Port ID (outgoing port): GigabitEthernet0/49
IP address: 148.000.000.61
Device ID: CIVIL_253
IP address: 148.000.000.253
Interface: GigabitEthernet1/0/4, Port ID (outgoing port): GigabitEthernet1/0/52
IP address: 148.000.000.253
Device ID: Arquitectura
IP address: 148.000.000.253
Interface: GigabitEthernet1/0/3, Port ID (outgoing port): GigabitEthernet0/1
IP address: 148.000.000.253
Device ID: ING_CIVIL
IP address: 148.000.000.251
Interface: GigabitEthernet1/0/7, Port ID (outgoing port): GigabitEthernet0/2
IP address: 148.000.000.251
Device ID: ING_CIVIL_DIR
IP address: 148.000.0.188
Interface: GigabitEthernet1/0/10, Port ID (outgoing port): GigabitEthernet0/2
Device ID: Ingenieria_Posgrado
IP address: 148.000.000.253
Interface: GigabitEthernet1/0/8, Port ID (outgoing port): GigabitEthernet0/1
IP address: 148.000.000.253
Device ID: Biblio_Barragan
IP address: 148.000.000.61
Interface: GigabitEthernet1/0/2, Port ID (outgoing port): GigabitEthernet0/1
IP address: 148.000.000.61
Device ID: Electronica_Edif_3
IP address: 148.000.000.253
Interface: GigabitEthernet1/0/5, Port ID (outgoing port): GigabitEthernet0/1
IP address: 148.000.000.253
我想要一个像这样的 .csv 文件:
device_id,ip_address
BIOTERIO, 148.000.00.189
N7K-LAN(JAF1651ANDL), 148.000.0.192
LAB_PESADO, 148.000.000.130
Arquitectura_Salones, 148.000.000.61
CIVIL_253, 148.000.000.253
Arquitectura, 148.000.000.253
ING_CIVIL, 148.000.000.251
ING_CIVIL_DIR, 148.000.0.188
Ingenieria_Posgrado, 148.000.000.253
Biblio_Barragan, 148.000.000.61
Electronica_Edif_3, 148.000.000.253
到目前为止我已经写了这个 awk 代码:
awk '
BEGIN {
RS = "\n\n"
FS = "\n"
OFS = ","
print "device_id,ip_address"
}
{
for(i=1; i<=NF; i++) {
split($i, a, ":");
k[a[1]] = a[2]
}
print k["Device ID"], k["IP address"]
}' tabladetallada.dat > dispositivoss.csv
但这就是我所拥有的:
device_id,ip_address
Electronica_Edif_3
,
有什么帮助吗?
提前致谢。
答案1
没有必要花哨:
awk 'BEGIN{RS="Device ID:"}{print $1", "$4}' file
并将第一行替换为标题。