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

如何在Perl中组合两个文件?,perl,Perl,我想读入两个输入文件并输出一个新文件,其中包含一行,该行是两个输入文件中每个对应行的串联 例如: 新输出文件的第1行将具有: info from input file 1, line 1 some number of tabs info from input file 2, line 1 . . . 如果其中一个输入文件的行数多于另一个,则应将其余的行插入到输出文件的正确位置 谢谢。打开FP1,“文件名1”; open FP1,"filename1"; open FP2,"filename2"

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

例如:

新输出文件的第1行将具有:

info from input file 1, line 1 some number of tabs info from input file 2, line 1
.
.
.
如果其中一个输入文件的行数多于另一个,则应将其余的行插入到输出文件的正确位置

谢谢。

打开FP1,“文件名1”;
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;
打开FP2,“文件名2”; 我的($l1,$l2); 而(1) { $l1=;chomp$l1; $l2=;chomp$l2; 最后,除非(定义为$l1或定义为$l2); 打印$l1.$l2,“\n”; } 关闭FP2; 关闭FP1;
打开FP1,“文件名1”;
打开FP2,“文件名2”;
我的($l1,$l2);
而(1)
{
$l1=;chomp$l1;
$l2=;chomp$l2;
最后,除非(定义为$l1或定义为$l2);
打印$l1.$l2,“\n”;
}
关闭FP2;
关闭FP1;
您可以首先查询(使用
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);
使用严格;
使用警告;
打开(my$fh,“您可以首先查询(使用
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);
使用严格;
使用警告;

open(my$fh,“这与在一个文件中循环没有什么不同,只要您注意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的一些技巧。 对于一个文件,它是常用的

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
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";
}
#!perl
严格使用;
使用警告;
my($file1,$file2)=@ARGV;
die“用法:$0 file1 file2\n”
除非$file1和$file2;
使用File::Slurp;
我的@a=read_文件($file1)
或者死“无法读取$file1-$!”;
my@b=read_文件($file2)
或者死“无法读取$file2-$!”;
我的$combled={};#hashref
我的$i=0;
foreach(@a){
咀嚼;
$combined->{$i}{b}='',除非定义了$combined->{$i}{b};
$combined->{$i++}{a}=$\;
}
$i=0;
foreach(@b){
咀嚼;
$combined->{$i}{a}='',除非定义了$combined->{$i}{a};
$combined->{$i++}{b}=$;
}
foreach my$i(排序{$a$b}键%$组合){
打印$combled->{$i}{a},(“\t”x2),$combled->{$i}{b},“\n”;
}

我喜欢用散列来聚合东西。这很快,但不是特别优雅

#!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";
}
#!perl
严格使用;
使用警告;
my($file1,$file2)=@ARGV;
die“用法:$0 file1 file2\n”
除非$file1和$file2;
使用File::Slurp;
我的@a=read_文件($file1)
或者死“无法读取$file1-$!”;
my@b=read_文件($file2)
或者死“无法读取$file2-$!”;
我的$combled={};#hashref
我的$i=0;
foreach(@a){
咀嚼;
$combined->{$i}{b}='',除非定义了$combined->{$i}{b};
$combined->{$i++}{a}=$\;
}
$i=0;
foreach(@b){
咀嚼;
$combined->{$i}{a}='',除非定义了$combined->{$i}{a};
$combined->{$i++}{b}=$;
}
foreach my$i(排序{$a$b}键%$组合){
打印$combled->{$i}{a},(“\t”x2),$combled->{$i}{b},“\n”;
}
#!/usr/bin/env perl
#合并第一个文件的3行和第二个文件的3行以及下一个文件。
打开(F1,“$file”)或死亡“\n无法写入文件\n”;
$value=0;
foreach$nums(@lines1){
如果($value%3==0){
打印F3$lines2[$value];
打印F3$lines2[$value+1];
打印F3$lines2[$value+2];
}
打印F3$nums;
$value++;
}
关闭(F3);
#!/usr/bin/env perl
#合并第一个文件的3行和第二个文件的3行以及下一个文件。
打开(F1,“$file”)或死亡“\n无法写入文件\n”;
$value=0;
foreach$nums(@lines1){
如果($value%3==0){
打印F3$lines2[$value];
打印F3$lines2[$value+1];
打印F3$lines2[$value+2];
}
打印F3$nums;
$value++;
}
关闭(F3);

你做了什么研究,在哪里卡住了?你是在*NIX系统上吗?
perl-e'exec@ARGV'paste/tmp/file1/tmp/file2
:)PercCro:是的,我在*NIX上,你的解决方案工作得很好。我不敢相信我没有做这件事。我在发布之前做了研究,但是忘了考虑粘贴命令。谢谢。你做了什么研究,你卡在哪里?你在A*NIX系统上吗?<代码> Perl -e Excel @ ARGV’PAST/TMP/FIL1/TMP/FIL2:@皮尔克罗:是的,我是尼克斯,你的解决方案很有效。我不敢相信我自己没有想到这个。我做了研究之前张贴,但忘了考虑粘贴命令。谢谢,谢谢。这是非常有用的。请不要提供使用bareword文件句柄的示例代码,或open的2 arg格式,或忽略检查open的返回。所有这些都是非常令人沮丧的做法。@Ven'Tatsu:…我没有说“使用严格;使用警告”;我也没有处理文件无法打开的情况,等等。等等。对于任何Perl脚本,您总是可以唠叨上千件事,因为它不能满足某些人的口味。请不要这么挑剔@豆腐,我有没有抱怨过
使用严格的
?没有。我确实抱怨过3个具体问题,这些问题在很多示例代码中都是不应该出现的,它们几乎不需要任何努力就可以正确地完成,但当它们做错时,可能会造成巨大的麻烦。我特别注意到,但没有评论一些“挑剔”的个人口味问题,因为我不想分散我所认为的严重问题。这是非常有用的。请不要提供使用bareword文件句柄的示例代码,或open的2 arg格式,或忽略检查open的返回。所有这些都是非常令人沮丧的做法。@Ven'Tatsu:…我没有说“使用严格;使用警告”;我也没有处理文件无法打开的情况,等等。等等。有一千个