0

how can I put 2 files A and B with the same number of lines side by side with Perl with command line ? I cannot work with two filehandles on the command line.

2
  • Could you elaborate a bit on what your trying to do. It sounds like this might be homework.
    – Brian
    Dec 14, 2021 at 1:13
  • Read the first file into e.g. a list of lines, then read the second file into e.g. a list of lines and then print each "matching" element from each list one after the other e.g. list1-1 list2-1 (side-by-side)? Dec 14, 2021 at 6:42

1 Answer 1

0

Here is a way to go:

I used those 2 files:

$ cat file1
file1 line1
file1 line2
file1 line3

$ cat file2
file2 line1
file2 line2
file2 line3

Running one-liner:

$ perl -E '($f1,$f2)=@ARGV;open $F1,$f1;chomp(@l1=<$F1>);open $F2,$f2;chomp(@l2=<$F2>);for($i=0;$i<@l1;$i++){say $l1[$i],"\t",$l2[$i]}' file1 file2
file1 line1     file2 line1
file1 line2     file2 line2
file1 line3     file2 line3
($f1,$f2)=@ARGV;                # retrieve the 2 filenames
open $F1,$f1;                   # open first file
chomp(@l1=<$F1>);               # read it in an array
open $F2,$f2;                   # open second file
chomp(@l2=<$F2>);               # read it in an array
for($i=0;$i<@l1;$i++){          # loop thru all record (as said in question the 2 file have same number of records
    say $l1[$i],"\t",$l2[$i]        # print line by line the 2 file content separated by a tabulation
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.