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:


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

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

Device ID: SEP0c116722f331
IP address:

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

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


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

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

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

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

Device ID: CIVIL_253
IP address:
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:


     P7K08UQ         , BIOTERIO,
     H006K022        , N7K-LAN(JAF1651ANDL),
     FNS174002FT     , LAB_PESADO,
     FNS14420533     , Arquitectura_Salones,
     H006K021        , CIVIL_253,
     H006K083        , Arquitectura,
     H006K032        , ING_CIVIL,
     FNS16361SG0     , ING_CIVIL_DIR,
     H006K040        , Ingenieria_Posgrado,
     00000MTC1444080Z, Biblio_Barragan,
     FNS11190FLE     , Electronica_Edif_3,

But in Example_1 I get this output:

, CIVIL_3702-01,
, SEP0c1167231e23,
, SEP0c116722f331,
, SEP0c116722f197,
 OPC13020953     , Barragan_3750,

In Example_1 I need an output like this:

 OPC13020953     , Barragan_3750,

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

  • 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


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 .