To demonstrate the process, I prepared two files, a and b (it would have been kind of you to prepare them for us).

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;

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: selecting corresponding lines in multiple files by choroba
in thread selecting corresponding lines in multiple files by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.