如何在Perl中组合两个文件?

时间:2010-12-29 18:41:38

标签: perl

我想读入两个输入文件并输出一个新文件,其中包含一行,它是两个输入文件中每个对应行的串联。

例如:

新输出文件的第1行将包含:

info from input file 1, line 1 some number of tabs info from input file 2, line 1
.
.
.

如果任一输入文件的行数多于另一行,则其余行应以正确的位置插入输出文件中。

感谢。

5 个答案:

答案 0 :(得分:2)

open FP1,"filename1";
open FP2,"filename2";
my ($l1,$l2);
while(1)
{
  $l1=<FP1>; chomp $l1;
  $l2=<FP2>; chomp $l2; 
  last unless(defined $l1 or defined $l2);
  print $l1.$l2,"\n";
}
close FP2;
close FP1;

答案 1 :(得分:2)

我喜欢哈希来聚合事物。这很快,即使不是特别优雅。

#!perl
use strict; 
use warnings;

my ($file1, $file2) = @ARGV;
die "usage: $0 file1 file2\n" 
    unless $file1 && $file2;

use File::Slurp;
my @a = read_file($file1)
    or die "couldn't read $file1 - $!";
my @b = read_file($file2)
    or die "couldn't read $file2 - $!";

my $combined = {}; # hashref

my $i=0;
foreach (@a) {
    chomp;
    $combined->{$i}{b} = '' unless defined $combined->{$i}{b};
    $combined->{$i++}{a} = $_;
}

$i=0;
foreach (@b) {
    chomp;
    $combined->{$i}{a} = '' unless defined $combined->{$i}{a};
    $combined->{$i++}{b} = $_;
}

foreach my $i (sort {$a<=>$b} keys %$combined) {
    print $combined->{$i}{a}, ("\t" x 2), $combined->{$i}{b}, "\n";
}

答案 2 :(得分:1)

只要你注意一些Perl的技巧,循环浏览一个文件就没那么了。 对于一个文件,通常使用

use strict;
use warnings;
use English qw(-no_match_vars);

my $filename = 'foo';
open my $file, '<', $filename or die "Failed to open '$filename' $OS_ERROR\n";

while (my $line = <$file>) {
    # work with $line
}

close $file;

这可以通过打开两个文件并将循环条件更改为仅在两个文件都已完成读取时结束来扩展为两个文件。 但是有一个问题,当Perl看到一个简单的文件句柄读取作为while循环的条件时它将它包装在defined()中,因为条件现在不仅仅是一个简单的读取,这需要完成手动

use strict;
use warnings;
use English qw(-no_match_vars);

my $filename1 = 'foo';
my $filename2 = 'bar';
open my $file1, '<', $filename1 or die "Failed to open '$filename1' $OS_ERROR\n";
open my $file2, '<', $filename2 or die "Failed to open '$filename2' $OS_ERROR\n";

my ($line1, $line2);
while ( do { $line1 = <$file1>; $line2 = <$file2>; defined($line1) || defined($line2) } ) {
    # do what you need to with $line1 and $line2
}

close $file1;
close $file2;

答案 3 :(得分:0)

您可以先查询(使用wc -l)哪个文件包含更多行。假设(为了伪代码)文件1有更多行,那么执行以下操作:

use strict;
use warnings;

open(my $fh,"<","file 1") or die ("Couldn't open file 1: $!");
open(my $write,">","output.csv") or die ("Couldn't open output.csv: $!");

my $str;
my $count=1;

while(my $line=<$fh>)
{
   $str=`head -n $count file 2 | tail -n 1`. (\tx[however many tabs you want]) . $line;
   print $write $str;
   $count++;
}

close($fh);
close($write);

答案 4 :(得分:0)

#!/usr/bin/env perl

#merging 3 - lines of first file and 3 lines of second file and next of these.
open(F1, "<file1") or die "\ncould not find your file1\n";

my@lines1;@lines1 = < F1 > ;

close(F1);

open(F2, "<file2") or die "\ncould not find your file2\n";

my@lines2;@lines2 = < F2 > ;

close(F2);

my $value;
my $nums;

print "\nplease write your output file name::::\n";

chomp($file = < STDIN > );

open(F3, "> $file") or die "\n could not write into your file\n";

$value = 0;
foreach $nums(@lines1) {


    if ($value % 3 == 0) {

        print F3 $lines2[$value];
        print F3 $lines2[$value + 1];
        print F3 $lines2[$value + 2];

    }
    print F3 $nums;
    $value++;

}
close(F3);