Q:

Extract matching pattern from input file and print to output file in Perl

I have huge input file from ncbi blastn in this form:
<ul id="msgFrm" class=" msg">
Job Title: otu0                  
Database: rRNA_typestrains/prokaryotic_16S_ribosomal_RNA 16S ribosomal RNA (Bacteria and Archaea)
Query #1: otu0 Query ID: lcl|Query_16950 Length: 460

Sequences producing significant alignments:
                                                                  Max    Total Query   E   Per.                  
Description                                                       Score  Score cover Value Ident  Accession        
Bacteroides dorei strain 175 16S ribosomal RNA, partial sequence  839    839   100%  0.0   99.57  NR_041351.1      

Alignments:
>Bacteroides dorei strain 175 16S ribosomal RNA, partial sequence
Sequence ID: NR_041351.1 Length: 1493 
Range 1: 341 to 800

Score:839 bits(454), Expect:0.0, 
Identities:458/460(99%),  Gaps:0/460(0%), Strand: Plus/Plus

Query  1    CCTACGGGGGGCAGCAGTGAGGAATATTGGTCAATGGGCGATGGCCTGAACCAGCCAAGT  60
            |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||
Sbjct  341  CCTACGGGAGGCAGCAGTGAGGAATATTGGTCAATGGGCGATGGCCTGAACCAGCCAAGT  400

Query #2: otu1 Query ID: lcl|Query_16951 Length: 460

Sequences producing significant alignments:
                                                                  Max    Total Query   E   Per.                  
Description                                                       Score  Score cover Value Ident  Accession          
Bacteroides vulgatus ATCC 8482 16S ribosomal RNA, partial...      811    811   100%  0.0   98.48  NR_074515.1      
Bacteroides paurosaccharolyticus strain WK042 16S ribosomal RN... 673    673   100%  0.0   93.06  NR_112668.1      

Alignments:
>Bacteroides dorei strain 175 16S ribosomal RNA, partial sequence
Sequence ID: NR_041351.1 Length: 1493 
Range 1: 341 to 800

Score:833 bits(451), Expect:0.0, 
Identities:457/460(99%),  Gaps:0/460(0%), Strand: Plus/Plus

Query  1    CCTACGGGGGGCAGCAGTGAGGAATATTGGTCAATGGGCGATGGCCTGAACCAGCCAAGT  60
            |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||
Sbjct  341  CCTACGGGAGGCAGCAGTGAGGAATATTGGTCAATGGGCGATGGCCTGAACCAGCCAAGT  400

Query #3: otu2 Query ID: lcl|Query_16952 Length: 460

Sequences producing significant alignments:
                                                                  Max    Total Query   E   Per.                  
Description                                                       Score  Score cover Value Ident  Accession             
Bacteroides sartorii JCM 16497 16S ribosomal RNA, partial...      684    684   100%  0.0   93.48  NR_113064.1      
Bacteroides paurosaccharolyticus strain WK042 16S ribosomal RN... 678    678   100%  0.0   93.28  NR_112668.1      

Alignments:
>Bacteroides dorei strain 175 16S ribosomal RNA, partial sequence
Sequence ID: NR_041351.1 Length: 1493 
Range 1: 341 to 800

Score:839 bits(454), Expect:0.0, 
Identities:458/460(99%),  Gaps:0/460(0%), Strand: Plus/Plus

Query  1    CCTACGGGTGGCAGCAGTGAGGAATATTGGTCAATGGGCGATGGCCTGAACCAGCCAAGT  60
            |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||
Sbjct  341  CCTACGGGAGGCAGCAGTGAGGAATATTGGTCAATGGGCGATGGCCTGAACCAGCCAAGT  400

...and so one.
Now, for each "Query #" in the file, I need to extract the paragraph information included between "Query #" and "Alignments" and print in another output file.
I tried with the following basic code but I am encountering some difficulties in Perl programming .  Can someone help me in resolving the issue? Thank you a lot
#!/usr/bin/perl -w

use warnings;

open(GENBANK, "Bact_ncbi_output.txt") or die;

my $outfile = 'output.txt';
open OUT,'>',$outfile 
  or die "Could not open $outfile : $!";

$content = join("", <GENBANK>);   
close GENBANK;

$content =~ /Query\s+([A-Z0-9_.]+)Alignments$/;         

print OUT "$content\n";

A:

Please try this:
#!/usr/bin/perl -w

use warnings;

open(GENBANK, "txt.txt") or die "";
local $/; $_= <GENBANK>; my $content = $_;

my $outfile = 'output.txt';
open(OUT,">",$outfile) or die "Could not open $outfile : $!";
my (@fetch) = $content=~m{(Query\s*\#((?:(?!Alignments\:\n).)*)\nAlignments\:\n)}gs;
#print OUT @fetch;
print OUT join "\n", @fetch;

Please change wherever you need.
Thanks.

