http://qs321.pair.com?node_id=1057469


in reply to Print a previous to previous of a matching line

Anonymous Monk's suggestion is the right one, just a bit elliptic. Let's phrase this another way: I didn't write out the code because I think it might be more useful to you to write the code yourself. You shouldn't need anything more complicated than one variable to keep the query line in, another one to read the next line from <STDIN> into, a while() loop to keep reading until you're out of lines, and an a couple of if() statements (is this line a "query" line, is this line a "hits" line, does this "hits" line meet my "I want the last 'Query' line" criterion) inside the loop. You don't even need a trailing check outside the loop, because a "hits" line always follows a "Query" line.
  • Comment on Re: Print a previous to previous of a matching line

Replies are listed 'Best First'.
Re^2: Print a previous to previous of a matching line
by ag88 (Novice) on Oct 11, 2013 at 07:49 UTC

    Thankyou all for the suggestions. It was really helpful. The following code did my task

    sub getGiForZeroHits { my $previous1,$previous2; open(FILEOUT,">giForZeroHits.txt") or die("Cannot open file"); { open(FILE, "$inputSeqFileForBlast-1e-10.out") or die("Cannot open file +"); { while(<FILE>) { my $line = $_; if($line=~/# 0 hits found/) { my @lineSpl = split(/\|/, $previous2); print FILEOUT "$lineSpl[1]\n"; } #close if $previous2= $previous1; $previous1= $line; } #close while close(FILE); } #close FILE close(FILEOUT) } #close FILEOUT } #close sub