Skip to main content
deleted 4 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

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.

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: 

Expected output: File1 for 16S:

start description Human 16S rRNA some text some text // File2 for 23S: 

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); 

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.

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); 

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.

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); 

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.

Input file:

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:  

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); 
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); 

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.

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); 
Source Link

Perl code for splitting a file, if 16s and 23s is present. and copy into a single file

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);