in reply to can't read the 2nd input file
Since you provided your code and sample input and output data, I'll give you some code to get you started:
#!/usr/bin/env perl use warnings; use strict; use Data::Dumper; # for debugging $Data::Dumper::Useqq=1; my $DEBUG = 1; die "Usage: $0 <INPUT_FILE_ports> <INPUT_FILE_IP> <OUTPUT_FILE>" if @ARGV!=3; my ($PORTS_FILE, $IPS_FILE, $OUT_FILE) = @ARGV; open my $ip_fh, '<:crlf', $IPS_FILE or die "$IPS_FILE: $!"; chomp( my @ips = <$ip_fh> ); close $ip_fh; print Data::Dumper->Dump([\@ips], ['*ips']) if $DEBUG; open my $out_fh, '>', $OUT_FILE or die "$OUT_FILE: $!"; open my $port_fh, '<:crlf', $PORTS_FILE or die "$PORTS_FILE: $!"; my $header = <$port_fh>; while ( my $line = <$port_fh> ) { chomp($line); my ($line_nr,$rest) = $line =~ /^(\d+)(\s+.+)$/ or die "Failed to parse line: $_"; print Data::Dumper->Dump([$line_nr,$rest], [qw/line_nr rest/]) if $DEBUG; die "Invalid line number: $line_nr" if $line_nr < 1 || $line_nr > @ips; my $ip = $ips[ $line_nr - 1 ]; print Data::Dumper->Dump([$ip], ['ip']) if $DEBUG; print {$out_fh} $ip, $rest, "\n"; } close $port_fh; close $out_fh;
Note that this makes a few small assumptions:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: can't read the 2nd input file
by perl_boy (Novice) on Feb 03, 2022 at 16:20 UTC |