打印匹配后的下一行。

huangapple go评论122阅读模式
英文:

Print the next line after match

问题

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

英文:

My input:

# 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

My code:

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

My output:

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

I want to print the next line in place of
"1 hits found: # Query: ;HaelonEVm219174t4;"

Expected output:

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

答案1

得分: 5

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

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
英文:

When the regex matches, set the flag but don't print anything for that line. When the flag is set, print the current line, then clear the flag.

use warnings;
use strict;

my $flag = 0;
while (&lt;DATA&gt;) {
    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

Prints:

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

答案2

得分: 2

你已经朝着设定标志位的正确方向迈出了一步。但是,不要立即打印,而是在标志位为真时打印。

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/;
}
英文:

You're on the right track with setting a flag. But instead of printing immediately, print when the flag is true.

my $flag = 0;
while (&lt;&gt;) {
   if ( $flag ) {
      print $line;
       $flag = 0;
   }

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

Simplified version:

my $flag = 0;
while (&lt;&gt;) {
   print if $flag;
   $flag = /\s1 hit/;
}

huangapple
  • 本文由 发表于 2023年8月4日 02:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定