http://www.perlmonks.org?node_id=926485


in reply to Merging larges files by columns

If think you want something like this. (untested)
open my $OUTFILE, ">", "outfile.csv" or die "oh noo: $!"; open my $IN1, "<", "infile1.csv" or die "bummer1: $!"; open my $IN2, "<", "infile2.csv" or die "bummer2: $!"; while (<$IN1>){ chomp; print $OUTFILE $_; my $line2 =<$IN2>; chomp $line2; my @f = split/,/, $line2; print $OUTFILE ",$f[1]\n"; } close $IN2 or die $!; close $IN1 or die $!; close $OUTFILE or die $!;

Replies are listed 'Best First'.
Re^2: Merging larges files by columns
by Kc12349 (Monk) on Sep 16, 2011 at 21:06 UTC

    I was about to post my very similar code. I would point out that if you are new to perl and not set in your ways, try to use lexical file handles as in my example as opposed to barewords.

    use autodie; open (my $out, '>', 'outfile.csv'); open (my $in1, '<', 'infile1.csv'); open (my $in2, '<', 'infile2.csv'); while ( defined (my $line = <$in1>) ) { chomp $line; print {$out} $line; chomp(my $in2_line = <$in2>); my @f = split m/,/, $in2_line; print {$out} ',' . $f[1] . "\n"; } close $in1; close $in2; close $out;