Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl中连接两个文件中的对应行?_Perl_Input - Fatal编程技术网

如何在Perl中连接两个文件中的对应行?

如何在Perl中连接两个文件中的对应行?,perl,input,Perl,Input,file1.txt hello tom well file2.txt world jerry done 如何将file1.txt与file2.txt合并;然后创建一个新文件-file3.txt hello world tom jerry well done 感谢您的阅读和回复 附上基于答案的完整代码 #!/usr/bin/perl use strict; use warnings; open(F1,"<","1.txt") or die "Cannot open file1:$!\

file1.txt

hello
tom
well
file2.txt

world
jerry
done
如何将file1.txt与file2.txt合并;然后创建一个新文件-file3.txt

hello world
tom jerry
well done
感谢您的阅读和回复

附上基于答案的完整代码

#!/usr/bin/perl
use strict;
use warnings;

open(F1,"<","1.txt") or die "Cannot open file1:$!\n"; 
open(F2,"<","2.txt") or die "Cannot open file2:$!\n";
open (MYFILE, '>>3.txt');

while(<F1>){ 
  chomp; 
  chomp(my $f2=<F2>); 
  print MYFILE $_ . $f2 ."\n"; 
} 
#/usr/bin/perl
严格使用;
使用警告;

打开(F1),如果Perl不是必须的,您可以在*nix上使用
粘贴
。如果您在Windows上,您也可以使用
粘贴
。只需从

否则,使用Perl

open(F1,"<","file1") or die "Cannot open file1:$!\n";
open(F2,"<","file2") or die "Cannot open file2:$!\n";
while(<F1>){
  chomp;
  chomp($f2=<F2>);
  print $_ . $f2 ."\n";
}

open(F1),我认为任何人都不应该给出完整的答案

只需打开两个文件,然后同时循环两个文件并写入一个新文件

如果您不知道如何用perl读取和写入文件,下面是一个教程:


它必须是perl还是linux?你可以使用$paste file1.txt file2.txtDup:perl和WinXP。$paste?这是来自linux的命令吗?对不起-这里有什么问题。@justintime,我根据ghostdog74的答案将代码附加到我的文章中。谢谢你。谢谢你的教程。我完成了脚本。
open(F1,"<","file1") or die "Cannot open file1:$!\n";
open(F2,"<","file2") or die "Cannot open file2:$!\n";
while(<F1>){
  chomp;
  chomp($f2=<F2>);
  print $_ . $f2 ."\n";
}