안녕하세요 여러분, 저는 이 스크립트를 사용해왔습니다:
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
이 두 파일을 사용합니다.
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
모든 것이 순조롭게 진행되었지만 결국 스크립트는 다음과 같이 SN
작성되었습니다 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
어떻게 보관하나요 Port ID
? , 때로는 dispositivos.dat
하나의 장치 정보에서만 동일한 상황이 발생합니다.
감사해요.
답변1
경로 변경:
$_ = "SN: $h{$a}" . ( eof() ? "" : "\n" );
도착하다
$_ .= (eof() ? "\n" : "") . "SN: $h{$a}" . ( eof() ? "" : "\n" );
답변2
perl -F',\s+' -l -00nae '
@ARGV and %h = (%h, reverse(/^(.*?),\s*(.*),/mg)),next;
/^Device ID:\s+(\S+)/ && exists $h{$a=$1} && s/$/\nSN: $h{$a}/;
print $_, eof() ? "": "\n";
' dispositivoss.csv dispositivos.dat > dispositivoss.dat
한 번에 한 단락씩 데이터를 볼 때.