如何使用 Perl 将两个行数相同的文件并排放置

如何使用 Perl 将两个行数相同的文件并排放置

我怎样才能使用 Perl 命令行将两个行数相同的文件 A 和 B 并排放置?我无法在命令行上使用两个文件句柄。

答案1

以下是一种方法:

我使用了这两个文件:

$ cat file1
file1 line1
file1 line2
file1 line3

$ cat file2
file2 line1
file2 line2
file2 line3

运行一行:

$ 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
}

相关内容