Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Print a previous to previous of a matching line

by pemungkah (Priest)
on Oct 08, 2013 at 23:53 UTC ( #1057469=note: print w/replies, xml ) Need Help??

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:
  • You're going through the file a line at a time.
  • If you see a line you might want (a "Query" line), you save it in a variable and keep reading lines.
  • If you see a "hits" line that matches your criterion, the line you saved was one you want. Print it or stick it in an array for later, or...
  • If you see a "hits" line and it doesn't match your criterion, then you don't want the "Query" you saw previously. Throw it away by setting the variable to "".
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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1057469]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2023-04-01 16:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found