1

I've got a file called dispositivos.dat in wich I have all the information related to devices directly connected to certain cisco equipment, depending on the equipment, the file changes, those are examples of this file:

Example_1:

Device ID: CIVIL_3702-01
IP address: 148.000.000.140
Interface: FastEthernet0/47
Port ID (outgoing port): GigabitEthernet0

Device ID: SEP0c1167231e23
IP address: 148.000.000.149
Interface: FastEthernet0/16
Port ID (outgoing port): Port 1

Device ID: SEP0c116722f331
IP address: 148.000.000.162

Device ID: SEP0c116722f197
IP address: 148.000.000.155
Interface: FastEthernet0/8
Port ID (outgoing port): Port 1

Device ID: Barragan_3750
IP address: 148.000.000.129
Interface: GigabitEthernet0/1
Port ID (outgoing port): GigabitEthernet1/0/11
SN: OPC13020953 

Example_2:

Device ID: BIOTERIO
IP address: 148.000.00.189
Interface: GigabitEthernet1/0/6
Port ID (outgoing port): GigabitEthernet0/1
SN: P7K08UQ         

Device ID: N7K-LAN(JAF1651ANDL)
IP address: 148.000.0.192
Interface: GigabitEthernet1/0/1
Port ID (outgoing port): Ethernet7/23
SN: H006K022        

Device ID: LAB_PESADO
IP address: 148.000.000.130
Interface: GigabitEthernet1/0/11
Port ID (outgoing port): GigabitEthernet0/1
SN: FNS174002FT     

Device ID: Arquitectura_Salones
IP address: 148.000.000.61
Interface: GigabitEthernet1/0/9
Port ID (outgoing port): GigabitEthernet0/49
SN: FNS14420533     

Device ID: CIVIL_253
IP address: 148.000.000.253
Interface: GigabitEthernet1/0/4
Port ID (outgoing port): GigabitEthernet1/0/52
SN: H006K021        

I'm using awk to create a csv file with this info, but as you can see I don't always have all the information, I'm specifically interested in SN (serial number) that is my primary key in my DB, the awk code I have is this:

awk '
    BEGIN {
        RS = "\n\n"
        FS = "\n"
        OFS = ","
        print "sn,device_id,ip_address"
    }
    {
        for(i=1; i<=NF; i++) {
            split($i, a, ":");
            k[a[1]] = a[2]
        }
        print k["SN"], k["Device ID"], k["IP address"]
    }' Example.dat > Example.csv

It works fine for Example_2 because every device has SN:

sn,device_id,ip_address

     P7K08UQ         , BIOTERIO, 148.000.00.189
     H006K022        , N7K-LAN(JAF1651ANDL), 148.000.0.192
     FNS174002FT     , LAB_PESADO, 148.000.000.130
     FNS14420533     , Arquitectura_Salones, 148.000.000.61
     H006K021        , CIVIL_253, 148.000.000.253
     H006K083        , Arquitectura, 148.000.000.253
     H006K032        , ING_CIVIL, 148.000.000.251
     FNS16361SG0     , ING_CIVIL_DIR, 148.000.0.188
     H006K040        , Ingenieria_Posgrado, 148.000.000.253
     00000MTC1444080Z, Biblio_Barragan, 148.000.000.61
     FNS11190FLE     , Electronica_Edif_3, 148.000.000.253
    FDO1129Z9ZJ,Barragan_3750,148.000.0.199

But in Example_1 I get this output:

sn,device_id,ip_address
, CIVIL_3702-01, 148.000.000.140
, SEP0c1167231e23, 148.000.000.149
, SEP0c116722f331, 148.000.000.162
, SEP0c116722f197, 148.000.000.155
 OPC13020953     , Barragan_3750, 148.000.000.129
FCQ1622X1GH,LAB_PESADO,148.000.000.130

In Example_1 I need an output like this:

sn,device_id,ip_address
 OPC13020953     , Barragan_3750, 148.000.000.129
 FCQ1622X1GH     ,LAB_PESADO,148.000.000.130

I need to avoid those devices without SN, I need the awk code to work in any case.

Could you help me?

Thanks in advance

4
  • Looks like you simply need to wrap your print statement inside an if that verifies that k["SN"] is not null.
    – DopeGhoti
    Mar 8, 2017 at 19:01
  • @DopeGhoti Nope, you also need to clear k["SN"] at the beginning of each record. Mar 8, 2017 at 19:09
  • or after the print, yes (:
    – DopeGhoti
    Mar 8, 2017 at 19:18
  • 1
    better delete the whole of k instead of just the SN field, in case some of the other fields are missing
    – ilkkachu
    Mar 8, 2017 at 19:30

1 Answer 1

3

A nonempty string is true, so you can just check k["SN"] for truthiness. Also, I would suggest clearing the whole of k at the beginning or end of the loop to avoid values from the previous item bleeding through.

So, replace the print line with something like:

if (k["SN"]) {
    print k["SN"], k["Device ID"], k["IP address"]
}
delete k;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .