如何使用perl合并两个文件的某些列?
我想合并input1.txt的第一列和input2.txt的第三列。我怎么做?我的代码不符合我的要求 输入1:如何使用perl合并两个文件的某些列?,perl,Perl,我想合并input1.txt的第一列和input2.txt的第三列。我怎么做?我的代码不符合我的要求 输入1: 1 6 2 7 3 8 4 9 a 4 8 b 6 7 c 3 4 d 2 6 1 8 2 7 3 4 4 6 #!/usr/bin/perl use strict; use warnings; open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $
1 6
2 7
3 8
4 9
a 4 8
b 6 7
c 3 4
d 2 6
1 8
2 7
3 4
4 6
#!/usr/bin/perl
use strict;
use warnings;
open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};
open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};
open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};
while(<$input1>)
{
my @columns1 = split;
print $outfile join("\t", $columns1[0], "\n");
}
while(<$input2>)
{
my @columns2 = split;
print $outfile join("\t", $columns2[2], "\n");
}
close($input1);
close($input2);
close($outfile);
输入2:
1 6
2 7
3 8
4 9
a 4 8
b 6 7
c 3 4
d 2 6
1 8
2 7
3 4
4 6
#!/usr/bin/perl
use strict;
use warnings;
open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};
open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};
open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};
while(<$input1>)
{
my @columns1 = split;
print $outfile join("\t", $columns1[0], "\n");
}
while(<$input2>)
{
my @columns2 = split;
print $outfile join("\t", $columns2[2], "\n");
}
close($input1);
close($input2);
close($outfile);
请求的输出:
1 6
2 7
3 8
4 9
a 4 8
b 6 7
c 3 4
d 2 6
1 8
2 7
3 4
4 6
#!/usr/bin/perl
use strict;
use warnings;
open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};
open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};
open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};
while(<$input1>)
{
my @columns1 = split;
print $outfile join("\t", $columns1[0], "\n");
}
while(<$input2>)
{
my @columns2 = split;
print $outfile join("\t", $columns2[2], "\n");
}
close($input1);
close($input2);
close($outfile);
我的代码:
1 6
2 7
3 8
4 9
a 4 8
b 6 7
c 3 4
d 2 6
1 8
2 7
3 4
4 6
#!/usr/bin/perl
use strict;
use warnings;
open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};
open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};
open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};
while(<$input1>)
{
my @columns1 = split;
print $outfile join("\t", $columns1[0], "\n");
}
while(<$input2>)
{
my @columns2 = split;
print $outfile join("\t", $columns2[2], "\n");
}
close($input1);
close($input2);
close($outfile);
#/usr/bin/perl
严格使用;
使用警告;
打开我的$input1,“执行以下操作:
$filename1 = $ARGV[0]; #for taking input1.txt as the first argument
$filename2 = $ARGV[1]; #for taking input2.txt as the second argument
@data1;
@column1;
open(INPUT_FILE, $filename1)
or die "Couldn't open $filename1!";
while (<INPUT_FILE>) {
my $currentLine = $_; #read the input file one line at a time, storing it to $currentLine
@data1 = split " ", $currentLine; #split your line by space
$firstcolumn = $data1[0]; #store the first column's data
push @column1, $firstcolumn ; #push the first column's data into an array
}
@data2;
@column3;
open(INPUT_FILE, $filename2)
or die "Couldn't open $filename2!";
while (<INPUT_FILE>) {
my $currentLine = $_;
@data2 = split " ", $currentLine;
$thirdcolumn = $data2[2]; #store the third column's data
push @column3, $thirdcolumn ;
}
$size = @column1;
open (OUTPUTFILE, '>>outfile.txt');
for($i = 0; $i < $size; $i++){
print OUTPUTFILE "$column1[$i] $column3[$i]\n"; #writing each entry into the outfile.txt
}
close(INPUT_FILE);
close (OUTPUTFILE);
$filename1=$ARGV[0]#用于将input1.txt作为第一个参数
$filename2=$ARGV[1]#用于将input2.txt作为第二个参数
@数据1;
@第1栏;
打开(输入文件,$filename1)
或者“无法打开$filename1!”;
而{
my$currentLine=$\u;#一次读取一行输入文件,并将其存储到$currentLine
@data1=split”“$currentLine;#按空格分割行
$firstcolumn=$data1[0];#存储第一列的数据
push@column1,$firstcolumn;#将第一列的数据推送到数组中
}
@数据2;
@第3栏;
打开(输入文件,$filename2)
或者死“无法打开$filename2!”;
而(){
我的$currentLine=$\;
@数据2=拆分“$currentLine;
$thirdcolumn=$data2[2];#存储第三列的数据
push@column3,$thirdcolumn;
}
$size=@column1;
打开(OUTPUTFILE,'>>outfile.txt');
对于($i=0;$i<$size;$i++){
打印输出文件“$column1[$i]$column3[$i]\n”#将每个条目写入outfile.txt
}
关闭(输入_文件);
关闭(输出文件);
在命令行中运行perl程序时,请执行以下操作:
yourprogram.pl input1.txt input2.txt outfile.txt
它应该会起作用
我尝试了这个程序并打开了outfile.txt,您请求的输出就在那里 获取请求输出的另一种方法是使用一个while循环,而不是两个:
mod.pl
#!/usr/bin/perl
use strict;
use warnings;
open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};
open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};
open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};
while(my $l1 = <$input1>){
my $l2 = <$input2>;
chomp $l1;
chomp $l2;
my @columns1 = split(/ /, $l1);
my @columns2 = split(/ /, $l2);
print $outfile join("\t", $columns1[1-1], $columns2[3-1]),"\n";
}
close($input1);
close($input2);
close($outfile);
您的代码是串行打印的,但您需要的是并行的
#!/usr/bin/perl
use strict;
use warnings;
open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};
open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};
open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};
my ($line1, $line2);
while(1)
{
$line1 = <$input1> || '';
$line2 = <$input2> || '';
my @columns1 = split ' ', $line1;
my @columns2 = split ' ', $line2;
print $outfile join("\t", $columns1[0], $columns2[2]), "\n";
last if !$line1 && !$line2;
}
close($input1);
close($input2);
close($outfile);
#/usr/bin/perl
严格使用;
使用警告;
打开我的$input1,“它不必这么复杂。读取数组中第一个文件的第一列,并将其与第二个文件的第三个字段一起打印。除非您有不同行数的文件,否则这应该可以正常工作
perl -lane'
BEGIN { $x = pop; @col1 = map { (split)[0] } <>; @ARGV = $x }
print join " ", $col1[$.-1], $F[-1]
' input1 input2
1 8
2 7
3 4
4 6
perl-lane'
BEGIN{$x=pop;@col1=map{(split)[0]};@ARGV=$x}
打印联接“%$col1[$.-1],$F[-1]
'输入1输入2
1 8
2 7
3 4
4 6
前两条打开的错误消息应该是“for reading”。