大家,我已经使用了这个脚本,到目前为止没有任何问题,它使用两个文件来创建 .csv,这两个文件来自另一个脚本,它包含有关连接到网络的设备的信息,这是导致错误时文件的外观。
文件1.dat:
SN: FCQ1632Y0UQ
Estadio_Admon
ip_address: 148.000.000.123
文件2.dat:
Device ID: ESTADIO_19
IP address: 148.000.000.119
Interface: FastEthernet0/3
Port ID (outgoing port): GigabitEthernet0
Device ID: ESTADIO_18
IP address: 148.000.000.118
Interface: FastEthernet0/4
Port ID (outgoing port): GigabitEthernet0
Device ID: ESTADIO_16
IP address: 148.000.000.116
Interface: FastEthernet0/6
Port ID (outgoing port): GigabitEthernet0
Device ID: ESTADIO_PALCOS
IP address: 148.000.000.66
Interface: GigabitEthernet0/2
Port ID (outgoing port): GigabitEthernet0/1
SN: FCQ1632Y0US
Device ID: ESTADIO_22
IP address: 148.000.000.122
Interface: FastEthernet0/8
Port ID (outgoing port): GigabitEthernet0
Device ID: SIPCCEF485DE89A
IP address: 148.000.000.92
Interface: FastEthernet0/16
Port ID (outgoing port): Port 1
Device ID: SIPCCEF485DE87B
IP address: 148.000.000.72
Interface: FastEthernet0/13
Port ID (outgoing port): Port 1
Device ID: SIPCCEF485E5719
IP address: 148.000.000.76
Interface: FastEthernet0/17
Port ID (outgoing port): Port 1
Device ID: SIPCCEF485DE894
IP address: 148.000.000.84
Interface: FastEthernet0/14
Port ID (outgoing port): Port 1
Device ID: ESTADIO_TAQUILLAS
IP address: 148.000.000.125
Interface: GigabitEthernet0/1
Port ID (outgoing port): GigabitEthernet1/0/27
SN: FOC1616Y091
脚本:
awk -v orig=$(awk '$1=="SN:" {print $2}' file1.dat) '
BEGIN {
RS = "\n\n"
FS = "\n"
OFS = ","
print "Device_SN_O,Device_SN_D,Interface,Port_ID"
}
{
for(i=1; i<=NF; i++) {
split($i, a, ": ");
k[a[1]] = a[2]
}
print orig, k["SN"], k["Interface"], k["Port ID (outgoing port)"]
}' file2.dat>final.csv
预期输出:
Device_SN_O,Device_SN_D,Interface,Port_ID
FCQ1632Y0UQ,,FastEthernet0/3,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/4,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/6,GigabitEthernet0
FCQ1632Y0UQ,FCQ1632Y0US,GigabitEthernet0/2,GigabitEthernet0/1
FCQ1632Y0UQ,,FastEthernet0/8,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/16,Port 1
FCQ1632Y0UQ,,FastEthernet0/13,Port 1
FCQ1632Y0UQ,,FastEthernet0/17,Port 1
FCQ1632Y0UQ,,FastEthernet0/14,Port 1
FCQ1632Y0UQ,FOC1616Y091,GigabitEthernet0/1,GigabitEthernet1/0/27
我得到的输出:
Device_SN_O,Device_SN_D,Interface,Port_ID
FCQ1632Y0UQ,,FastEthernet0/3,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/4,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/6,GigabitEthernet0
FCQ1632Y0UQ,FCQ1632Y0US,GigabitEthernet0/2,GigabitEthernet0/1
FCQ1632Y0UQ,FCQ1632Y0US,FastEthernet0/8,GigabitEthernet0
FCQ1632Y0UQ,FCQ1632Y0US,FastEthernet0/16,Port 1
FCQ1632Y0UQ,FCQ1632Y0US,FastEthernet0/13,Port 1
FCQ1632Y0UQ,FCQ1632Y0US,FastEthernet0/17,Port 1
FCQ1632Y0UQ,FCQ1632Y0US,FastEthernet0/14,Port 1
FCQ1632Y0UQ,FOC1616Y091,GigabitEthernet0/1,GigabitEthernet1/0/27
正如您所看到的,Device_SN_D 正在重复,直到找到不同的,我在不同的迭代中使用了相同的脚本,这是第一个给我这个错误的脚本。
希望你能帮我解决这个问题。
答案1
当您的数据没有任何 "SN: ....." 时,您不会为 k["SN"] 分配一个空值,因此最后一个值仍在其中。
您只需要添加一个 : delete k
(参见https://unix.stackexchange.com/a/147958/27616)就在处理下一行之前,以便使用“新鲜的 k 数组”处理下一行
举个例子:
awk -v orig=$(awk '$1=="SN:" {print $2}' file1.dat) '
BEGIN {
RS = "\n\n"
FS = "\n"
OFS = ","
print "Device_SN_O,Device_SN_D,Interface,Port_ID"
}
{
for(i=1; i<=NF; i++) {
split($i, a, ": ");
k[a[1]] = a[2]
}
print orig, k["SN"], k["Interface"], k["Port ID (outgoing port)"]
delete k; rem="So that the next line is processed with an emptied k array"
}' file2.dat>final.csv
根据您提供的数据,它在 Final.csv 中给出:
Device_SN_O,Device_SN_D,Interface,Port_ID
FCQ1632Y0UQ,,FastEthernet0/3,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/4,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/6,GigabitEthernet0
FCQ1632Y0UQ,FCQ1632Y0US,GigabitEthernet0/2,GigabitEthernet0/1
FCQ1632Y0UQ,,FastEthernet0/8,GigabitEthernet0
FCQ1632Y0UQ,,FastEthernet0/16,Port 1
FCQ1632Y0UQ,,FastEthernet0/13,Port 1
FCQ1632Y0UQ,,FastEthernet0/17,Port 1
FCQ1632Y0UQ,,FastEthernet0/14,Port 1
FCQ1632Y0UQ,FOC1616Y091,GigabitEthernet0/1,GigabitEthernet1/0/27
正如预期的那样