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-01IP address: 148.000.000.140Interface: FastEthernet0/47Port ID (outgoing port): GigabitEthernet0Device ID: SEP0c1167231e23IP address: 148.000.000.149Interface: FastEthernet0/16Port ID (outgoing port): Port 1Device ID: SEP0c116722f331IP address: 148.000.000.162Device ID: SEP0c116722f197IP address: 148.000.000.155Interface: FastEthernet0/8Port ID (outgoing port): Port 1Device ID: Barragan_3750IP address: 148.000.000.129Interface: GigabitEthernet0/1Port ID (outgoing port): GigabitEthernet1/0/11SN: OPC13020953 Example_2:        Device ID: BIOTERIOIP address: 148.000.00.189Interface: GigabitEthernet1/0/6Port ID (outgoing port): GigabitEthernet0/1SN: P7K08UQ         Device ID: N7K-LAN(JAF1651ANDL)IP address: 148.000.0.192Interface: GigabitEthernet1/0/1Port ID (outgoing port): Ethernet7/23SN: H006K022        Device ID: LAB_PESADOIP address: 148.000.000.130Interface: GigabitEthernet1/0/11Port ID (outgoing port): GigabitEthernet0/1SN: FNS174002FT     Device ID: Arquitectura_SalonesIP address: 148.000.000.61Interface: GigabitEthernet1/0/9Port ID (outgoing port): GigabitEthernet0/49SN: FNS14420533     Device ID: CIVIL_253IP address: 148.000.000.253Interface: GigabitEthernet1/0/4Port ID (outgoing port): GigabitEthernet1/0/52SN: 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.csvIt 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.199But 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.129FCQ1622X1GH,LAB_PESADO,148.000.000.130In 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.130I need to avoid those devices without SN, I need the awk code to work in any case.Could you help me?Thanks in advance
awk check condition and print just when it's true
shell script;awk;csv
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;