in reply to selecting corresponding lines in multiple files
File a contains abbreviations plus their type (day or month), tab separated:
Sun d Mon d Tue d Wed d Thu d Fri d Sat d Jan m Feb m Mar m Apr m May m Jun m Jul m Aug m Sep m Oct m Nov m Dec m
File b contains the full names:
Sunday Monday Tuesday Wednesday Thursday Friday Saturday January February March April May June July August September October November December
Perl makes it possible to keep several files opened at the same time, some for reading and some for writing. Let's only output the month to two output files. Note the checks that both the input files end at the same line, i.e. none of them is shorter.
#!/usr/bin/perl use warnings; use strict; sub is_month { my ($line) = @_; return $line =~ /\tm$/ ? 1 : 0 } open my $in_a, '<', 'a' or die "a: $!"; open my $in_b, '<', 'b' or die "b; $!"; open my $out_a, '>', 'a.out' or die "a.out: $!"; open my $out_b, '>', 'b.out' or die "b.out: $!"; while (my $line_a = <$in_a>) { my $line_b = <$in_b>; die "File b shorter!\n" unless defined $line_b; if (is_month($line_a)) { print {$out_a} $line_a; print {$out_b} $line_b; } } close $out_a; close $out_b; die "File a shorter!\n" unless eof $in_b;
|
|---|