我正在尝试将每个其他行合并到两个文件中。我尝试使用哈希来修改我的旧脚本,链接here。解释这一点的最好方法是通过展示一个例子,不能很好地说出来。如果您对我的任务有其他疑问,请在下面发表评论,我会澄清。
文件1:
>blue
it is a 2006 toyota
>red
it is a 1990
>black
it is a mongoose
>blue
it is a 2010
文件2:
>car
it is a 2006 toyota
>jeep
it is a 1990
>bike
it is a mongoose
>jeep
it is a 2010
预期产出:
>blue|car
it is a 2006 toyota
>red|jeep
it is a 1990
>black|bike
it is a mongoose
>blue|jeep
it is a 2010
我对旧脚本的问题在于它会删除任何重复的元素而我不想重复出现。因为在我的实际文件中,两个文件中的第二行是相同的(文件1中的第2行与文件2中的第2行相同)。我对perl one liners没有经验,但我认为一个衬垫可以比我的脚本更快地完成这项任务。
答案 0 :(得分:0)
#!/usr/bin/perl --
use warnings;
use strict;
open my $in1, "<", 'file1.txt' or die $!;
open my $in2, "<", 'file2.txt' or die $!;
while (<$in1>) {
chomp;
print;
my $file2line = <$in2>;
print "|", substr($file2line,1);
my $whatitis = <$in1> or last;
<$in2> || undef; # throw file2's line away
print $whatitis;
}
close $in1;
close $in2;
<强>输出:强>
$ more file1.txt
>blue
it is a 2006 toyota
>red
it is a 1990
>black
it is a mongoose
>blue
it is a 2010
$ more file2.txt
>car
it is a 2006 toyota
>jeep
it is a 1990
>bike
it is a mongoose
>jeep
it is a 2010
$ perl script2.pl
>blue|car
it is a 2006 toyota
>red|jeep
it is a 1990
>black|bike
it is a mongoose
>blue|jeep
it is a 2010
$