打印匹配后的下一行。

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

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:

  1. # BLASTN 2.13.0+
  2. # Query: ;HaelonEVm219174t4;
  3. # Database: /data1/IxoSca/Databases/nt/nt
  4. # Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
  5. # 1 hits found
  6. gi|1962480099|emb|LR990882.1| Endotricha flammealis 80.702 57 11 0 0.043 54.5 10 Endotricha flammealis genome assembly, chromosome: 29
  7. # BLASTN 2.13.0+
  8. # Query: ;HaelonEVm2775195t2;
  9. # Database: /data1/IxoSca/Databases/nt/nt
  10. # Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
  11. # 1 hits found
  12. 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:

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

My output:

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

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

Expected output:

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

答案1

得分: 5

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

  1. use warnings;
  2. use strict;
  3. my $flag = 0;
  4. while (<DATA>) {
  5. if (m/\s[1] hit/) {
  6. $flag = 1;
  7. }
  8. elsif ($flag) {
  9. print;
  10. $flag = 0;
  11. }
  12. }
  13. __DATA__
  14. # BLASTN 2.13.0+
  15. # Query: ;HaelonEVm219174t4;
  16. # Database: /data1/IxoSca/Databases/nt/nt
  17. # Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
  18. # 1 hits found
  19. gi|1962480099|emb|LR990882.1| Endotricha flammealis 80.702 57 11 0 0.043 54.5 10 Endotricha flammealis genome assembly, chromosome: 29
  20. # BLASTN 2.13.0+
  21. # Query: ;HaelonEVm2775195t2;
  22. # Database: /data1/IxoSca/Databases/nt/nt
  23. # Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
  24. # 1 hits found
  25. gi|2452922202|emb|OX438846.1| Eudonia truncicolella 88.372 43 3 1 0.009 53.6 29 Eudonia truncicolella genome assembly, chromosome: 21

打印:

  1. gi|1962480099|emb|LR990882.1| Endotricha flammealis 80.702 57 11 0 0.043 54.5 10 Endotricha flammealis genome assembly, chromosome: 29
  2. 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.

  1. use warnings;
  2. use strict;
  3. my $flag = 0;
  4. while (&lt;DATA&gt;) {
  5. if (m/\s[1] hit/) {
  6. $flag = 1;
  7. }
  8. elsif ($flag) {
  9. print;
  10. $flag = 0;
  11. }
  12. }
  13. __DATA__
  14. # BLASTN 2.13.0+
  15. # Query: ;HaelonEVm219174t4;
  16. # Database: /data1/IxoSca/Databases/nt/nt
  17. # Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
  18. # 1 hits found
  19. gi|1962480099|emb|LR990882.1| Endotricha flammealis 80.702 57 11 0 0.043 54.5 10 Endotricha flammealis genome assembly, chromosome: 29
  20. # BLASTN 2.13.0+
  21. # Query: ;HaelonEVm2775195t2;
  22. # Database: /data1/IxoSca/Databases/nt/nt
  23. # Fields: subject id, subject sci names, % identity, alignment length, mismatches, gap opens, evalue, bit score, % query coverage per hsp, subject title
  24. # 1 hits found
  25. gi|2452922202|emb|OX438846.1| Eudonia truncicolella 88.372 43 3 1 0.009 53.6 29 Eudonia truncicolella genome assembly, chromosome: 21

Prints:

  1. gi|1962480099|emb|LR990882.1| Endotricha flammealis 80.702 57 11 0 0.043 54.5 10 Endotricha flammealis genome assembly, chromosome: 29
  2. 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

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

  1. my $flag = 0;
  2. while (<>) {
  3. if ( $flag ) {
  4. print $line;
  5. $flag = 0;
  6. }
  7. if ( $line =~ m/\s1 hit/ ) {
  8. $flag = 1;
  9. }
  10. }

简化版本:

  1. my $flag = 0;
  2. while (<>) {
  3. print if $flag;
  4. $flag = /\s1 hit/;
  5. }
英文:

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

  1. my $flag = 0;
  2. while (&lt;&gt;) {
  3. if ( $flag ) {
  4. print $line;
  5. $flag = 0;
  6. }
  7. if ( $line =~ m/\s1 hit/ ) {
  8. $flag = 1;
  9. }
  10. }

Simplified version:

  1. my $flag = 0;
  2. while (&lt;&gt;) {
  3. print if $flag;
  4. $flag = /\s1 hit/;
  5. }

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:

确定