The subject line of your question contains the words "key values", that could have given you a hint that you can solve this problem using a hash :-).
Normally I recommend using Text::CSV_XS on CSV files, but this example data looks simple enough to handle without an extra module (reconsider using the module if your real datafiles should contain things like quoted data or escaped commas):
#!/usr/bin/perl use strict; use warnings; my %result; for my $file ("File_A","File_B","File_C") { open (my $fh,"<",$file) or die "Can't open file $file: $!"; <$fh>; # skip header line while (my $line = <$fh>) { my ($server,$cpua) = (split(",",$line))[0,1]; push @{$result{$server}},$cpua; } close $fh or die "Can't close file $file: $!"; } open (my $nfh,">","Result_File") or die "Can't open result file: $!"; for my $server (sort keys %result) { print $nfh $server.",".join(",",@{$result{$server}})."\n"; } close $nfh or die "Can't close result file: $!";
This assumes that you want the result file to contain the servers listed in alphabetical order. If you want a different ordering, simply construct an array holding the server names in the order you want them and use that for the final loop. For example
my @order = qw(wsompapgtw05 wsomddvfxa01 wsomdavpra03 WSOMQAVPRA05); for my $server (@order) { ...
In reply to Re: Combining Records From Multiple Files based on Common Key Values
by tirwhan
in thread Combining Records From Multiple Files based on Common Key Values
by country1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |