in reply to compare 2 files and print to a third
And the files -use strict; use IO::File; my %data; # hash to store data from first file my $f = new IO::File "first.txt", "r" or die "Can't open file 1"; while (<$f>) { chomp; my ($login) = /^(\w+):/; $data{$login} = $_; } undef $f; my $f = new IO::File "second.txt", "r" or die "Can't open file 2"; my $o = new IO::File "third.txt", "w" or die "Can't create file 3"; while (<$f>) { chomp; my ($login,$info) = /^(\w+)(:.*)/; if ($data{$login}) { print $o $data{$login} . $info, "\n"; } } undef $f; undef $o;
The output file ----- first.txt ---- 0001:rec1:rec2:rec3 0002:recA:recB:recC 0003:recX:recY:recZ ---- second.txt ---- 0001:rec4:rec5:rec6 0002:recD:recE:recF
---- third.txt ---- 0001:rec1:rec2:rec3:rec4:rec5:rec6 0002:recA:recB:recC:recD:recE:recF
|
|---|