Perl 从一个文件中跳过以##开头的行后读取两个文件
跳过一个文件中以##开头的行后,我想同时读取两个文件Perl 从一个文件中跳过以##开头的行后读取两个文件,perl,Perl,跳过一个文件中以##开头的行后,我想同时读取两个文件 file1.txt: ## ## ## header1 header2 file2.txt: header3 header4 有没有办法跳过###然后同时读取两个文件中的行 open IN1, "file1.txt"; open IN2, "file2.txt"; if <IN1> ^## skip while(my $one = <IN1>, my $two = <IN2>){ print
file1.txt:
##
##
##
header1 header2
file2.txt:
header3 header4
有没有办法跳过###然后同时读取两个文件中的行
open IN1, "file1.txt";
open IN2, "file2.txt";
if <IN1> ^## skip
while(my $one = <IN1>, my $two = <IN2>){
print "$one\t$two";
}
Outputs:
header1 header2 header1 header2
在1“file1.txt”中打开;
打开IN2,“file2.txt”;
如果跳过
而(我的$1=,我的$2=){
打印“$1\t$2”;
}
产出:
校长1校长2校长1校长2
在循环中,跳过file1的行,直到它们有效,并在其中一个文件完成时退出:
open my $file1, "<", "file1.txt" or die $!;
open my $file2, "<", "file2.txt" or die $!;
while (1) {
my $file1_line = <$file1> or next;
next if $file1_line =~ /^##/; # skip commented lines in file1
my $file2_line = <$file2> or next;
last if not ($file1_line and $file2_line);
chomp $file1_line;
chomp $file2_line;
print "$file1_line\t$file2_line\n";
}
close $file1;
close $file2;
在循环中,跳过file1的行,直到它们有效为止,并在其中一个文件完成时退出:
open my $file1, "<", "file1.txt" or die $!;
open my $file2, "<", "file2.txt" or die $!;
while (1) {
my $file1_line = <$file1> or next;
next if $file1_line =~ /^##/; # skip commented lines in file1
my $file2_line = <$file2> or next;
last if not ($file1_line and $file2_line);
chomp $file1_line;
chomp $file2_line;
print "$file1_line\t$file2_line\n";
}
close $file1;
close $file2;
单向:跳过每个文件,直到到达其标记;在新的循环中继续阅读它们
use warnings;
use strict;
use feature 'say';
my ($file1, $file2) = @ARGV;
die "Usage: $0 file1 file2\n" if !$file1 or !$file2;
open my $fh1, '<', $file1 or die "Can't open $file1: $!";
open my $fh2, '<', $file2 or die "Can't open $file2: $!";
# Second file's empty marker means it reads it from the beginning
my ($re_marker1, $re_marker2) = (qr/^##/, qr//);
while (<$fh1>) { last if /$re_marker1/ };
while (<$fh2>) { last if /$re_marker2/ };
while (1) {
my $l1 = <$fh1>;
my $l2 = <$fh2>;
chomp ($l1, $l2);
say "$l1 | $l2";
last if eof $fh1 or eof $fh2;
}
使用警告;
严格使用;
使用特征“说”;
my($file1,$file2)=@ARGV;
die“用法:$0 file1 file2\n”如果$文件1或$文件2;
打开我的$fh1,“单向:跳过每个文件,直到到达其标记;在新的循环中继续阅读它们
use warnings;
use strict;
use feature 'say';
my ($file1, $file2) = @ARGV;
die "Usage: $0 file1 file2\n" if !$file1 or !$file2;
open my $fh1, '<', $file1 or die "Can't open $file1: $!";
open my $fh2, '<', $file2 or die "Can't open $file2: $!";
# Second file's empty marker means it reads it from the beginning
my ($re_marker1, $re_marker2) = (qr/^##/, qr//);
while (<$fh1>) { last if /$re_marker1/ };
while (<$fh2>) { last if /$re_marker2/ };
while (1) {
my $l1 = <$fh1>;
my $l2 = <$fh2>;
chomp ($l1, $l2);
say "$l1 | $l2";
last if eof $fh1 or eof $fh2;
}
使用警告;
严格使用;
使用特征“说”;
my($file1,$file2)=@ARGV;
die“用法:$0 file1 file2\n”如果$文件1或$文件2;
打开我的$fh1,'在模式匹配中使用未初始化值$file1_行(m/)。确定如果没有可读取的内容,我已更新跳过循环。在模式匹配(m/)中使用未初始化值$file1_行。确定如果没有可读取的内容,我已更新跳过循环。