I have a file from which I want to search for the string "16S" and "23S" and extract the section containing these strings into two separate files.
#code
Input file:
start
description Human 16S rRNA
**some text**
**some text**
//
start
description Mouse 18S rRNA
some text
some text
//
start
description Mouse 23S rRNA
some text
some text
//
Expected output:
File1 for 16S:
start
description Human 16S rRNA
some text
some text
//
File2 for 23S:
start
description Mouse 23S rRNA
some text
some text
//
My code used:
#! /usr/bin/perl
# default output file is /dev/null - i.e. dump any input before
# the first [ entryN ] line.
$outfile='FullrRNA.gb';
open(OUTFILE,">",$outfile) || die "couldn't open $outfile: $!";
while(<>) {
# uncomment next two lines to optionally remove comments (startin with
# '#') and skip blank lines. Also removes leading and trailing
# whitespace from each line.
# s/#.*|^\s*|\s*$//g;
# next if (/^$/)
# if line begins with 'start', extract the filename
if (m/^\start/) {
(undef,$outfile,undef) = split ;
close(OUTFILE);
open(OUTFILE,">","$outfile.txt") || die "couldn't open $outfile.txt: $!";
} else {
print OUTFILE;
}
}
close(OUTFILE);