私は次のテキストファイルで作業しています。
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
最初の行をタイトルに置き換えます。