2

I'm working with this text file:

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

I would like to get a .csv file like this:

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

So far I've written this awk code:

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

But this is what I've got:

device_id,ip_address
 Electronica_Edif_3
,

Any help?

Thanks in advance.

1

There is no need to be fancy:

awk 'BEGIN{RS="Device ID:"}{print $1", "$4}' file

and just replace the first line with the header.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.