Hi everyone I have been working with this script:

```
    perl -F',\s+' -lane '
   @ARGV and $h{$F[1]}=$F[0],next;

   /^Device ID:\s+(\S+)/ && $h{$a=$1} .. /^$/ || eof() and do{
      /^$/ || eof() and $_ = "SN: $h{$a}" . ( eof() ? "" : "\n" );
   };

   print;
' dispositivoss.csv dispositivos.dat>dispositivoss.dat

```

It uses those 2 files.

dispositivoss.csv:

```
serial_number,device_id,ip_address
FOC1518Z1G2, Arq_Laboratorios_EdifB, 148.000.000.248
FOC1216U136, Arquitectura_Dir, 148.000.000.252
FOC1352V3F3, Arq.245, 148.000.000.245
FDO1129Z9Z5, Barragan_3750, 148.000.000.254

```

dispositivos.dat:

```
Device ID: Arq_Laboratorios_EdifB
IP address: 148.000.000.248
Interface: FastEthernet0/48
Port ID (outgoing port): GigabitEthernet1/0/48

Device ID: SEP0c1167231895
IP address: 148.000.000.45
Interface: FastEthernet0/4
Port ID (outgoing port): Port 1

Device ID: Arquitectura_Dir
IP address: 148.000.000.252
Interface: GigabitEthernet0/2
Port ID (outgoing port): GigabitEthernet0/1

Device ID: ARQUITECTURA_01
IP address: 148.000.000.21
Interface: FastEthernet0/1
Port ID (outgoing port): FastEthernet0

Device ID: Arq.245
IP address: 148.000.000.245
Interface: FastEthernet0/42
Port ID (outgoing port): GigabitEthernet0/1

Device ID: Barragan_3750
IP address: 148.000.000.254
Interface: GigabitEthernet0/1
Port ID (outgoing port): GigabitEthernet1/0/3

```

everything goes fine, but at the end, the script writes `SN` over `Port ID`:

```
Device ID: Arq_Laboratorios_EdifB
IP address: 148.000.000.248
Interface: FastEthernet0/48
Port ID (outgoing port): GigabitEthernet1/0/48
SN: FOC1518Z1G2

Device ID: SEP0c1167231895
IP address: 148.000.000.45
Interface: FastEthernet0/4
Port ID (outgoing port): Port 1

Device ID: Arquitectura_Dir
IP address: 148.000.000.252
Interface: GigabitEthernet0/2
Port ID (outgoing port): GigabitEthernet0/1
SN: FOC1216U136

Device ID: ARQUITECTURA_01
IP address: 148.000.000.21
Interface: FastEthernet0/1
Port ID (outgoing port): FastEthernet0

Device ID: Arq.245
IP address: 148.000.000.245
Interface: FastEthernet0/42
Port ID (outgoing port): GigabitEthernet0/1
SN: FOC1352V3F3

Device ID: Barragan_3750
IP address: 148.000.000.254
Interface: GigabitEthernet0/1
SN: FDO1129Z9Z5

```

How can I keep the `Port ID`?, sometimes `dispositivos.dat` has only one device information, and the same happens.

Thanks.