Search code examples
perl

Print the next line after match


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

Solution

  • 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 (<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
    

    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