perl 打印匹配后的下一行

7z5jn7bk  于 2023-08-06  发布在  Perl
关注(0)|答案(2)|浏览(203)

我的输入:

# BLASTN 2.13.0+
# Query: ;HaelonEVm219174t4;
# Database: /data1/IxoSca/Databases/nt/nt
# Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
# 1 hits found
gi|1962480099|emb|LR990882.1|   Endotricha flammealis   80.702  57  11  0   0.043    54.5   10  Endotricha flammealis genome assembly, chromosome: 29
# BLASTN 2.13.0+
# Query: ;HaelonEVm2775195t2;
# Database: /data1/IxoSca/Databases/nt/nt
# Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
# 1 hits found
gi|2452922202|emb|OX438846.1|   Eudonia truncicolella   88.372  43  3   1   0.009   53.6    29  Eudonia truncicolella genome assembly, chromosome: 21

字符串
我的代码:

}
elsif($line =~ m/\s[1] hit/){
    print "1 hits found: ".$name."\n";
            $flag=1;
}


我的输出:

HaelonEVm219174t4   1 hits found: # Query: ;HaelonEVm219174t4;
HaelonEVm2775195t2  1 hits found: # Query: ;HaelonEVm2775195t2;


我想打印下一行来代替 “找到1个匹配项:# Query:;HaelonEVm219174t4;“***
预期的输出:

HaelonEVm219174t4   LR990882.1   Endotricha flammealis   80.702  57  11  0   0.043    54.5   10  Endotricha flammealis genome assembly, chromosome: 29
HaelonEVm2775195t2  OX438846.1   Eudonia truncicolella   88.372  43  3   1   0.009   53.6    29  Eudonia truncicolella genome assembly, chromosome: 21

xam8gpfp

xam8gpfp1#

当正则表达式匹配时,设置标志,但不打印该行的任何内容。设置标志后,打印当前行,然后清除标志。

use warnings;
use strict;

my $flag = 0;
while (<DATA>) {
    if (m/\s[1] hit/) {
        $flag = 1;
    }
    elsif ($flag) {
        print;
        $flag = 0;
    }
}

__DATA__
# BLASTN 2.13.0+
# Query: ;HaelonEVm219174t4;
# Database: /data1/IxoSca/Databases/nt/nt
# Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
# 1 hits found
gi|1962480099|emb|LR990882.1|   Endotricha flammealis   80.702  57  11  0   0.043    54.5   10  Endotricha flammealis genome assembly, chromosome: 29
# BLASTN 2.13.0+
# Query: ;HaelonEVm2775195t2;
# Database: /data1/IxoSca/Databases/nt/nt
# Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
# 1 hits found
gi|2452922202|emb|OX438846.1|   Eudonia truncicolella   88.372  43  3   1   0.009   53.6    29  Eudonia truncicolella genome assembly, chromosome: 21

字符串
印刷品:

gi|1962480099|emb|LR990882.1|   Endotricha flammealis   80.702  57  11  0   0.043    54.5   10  Endotricha flammealis genome assembly, chromosome: 29
gi|2452922202|emb|OX438846.1|   Eudonia truncicolella   88.372  43  3   1   0.009   53.6    29  Eudonia truncicolella genome assembly, chromosome: 21

0sgqnhkj

0sgqnhkj2#

你在正确的轨道上设置一个标志。但不是立即打印,而是在标志为true时打印。

my $flag = 0;
while (<>) {
   if ( $flag ) {
      print $line;
       $flag = 0;
   }

   if ( $line =~ m/\s1 hit/ ) {
      $flag = 1;
   }
}

字符串
简化版:

my $flag = 0;
while (<>) {
   print if $flag;
   $flag = /\s1 hit/;
}

相关问题