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

G'day ag88,

Welcome to the monastery.

You can treat each BLASTP block as a single record. This makes it easy to identify which have "0 hits found", and print their "gi" values. (In the code below, I've truncated the data lines to 60 characters.)

#!/usr/bin/env perl -l use strict; use warnings; { local $/ = "# BLASTP 2.2.28+\n"; while (<DATA>) { print /gi\|(\d+)/ if /0 hits found/; } } __DATA__ # BLASTP 2.2.28+ # Query: gi|338220664|gb|EGP06123.1| hypothetical protein GE # Database: nr-25sep # Fields: query id, subject id, % identity, alignment length # 2 hits found gi|338220664|gb|EGP06123.1| gi|45383702|ref|NP_989542.1| gi|338220664|gb|EGP06123.1| gi|15419940|gb|AAK97214.1| 44.1 # BLASTP 2.2.28+ # Query: gi|338220666|gb|EGP06125.1| hypothetical protein GE # Database: nr-25sep # 0 hits found # BLASTP 2.2.28+ # Query: gi|338220651|gb|EGP06111.1| hypothetical protein GE # Database: nr-25sep # 0 hits found

Output:

338220666 338220651

See "perlvar: Variables related to filehandles" for a discussion of this usage of "$/" (the input record separator).

-- Ken