I am trying to extract entries from within a large Genbank file, with many thousands of entries. For a search string, I'm using a unique gene name – that works fine. The tricky bit is that I'd like to print the entire entry for that particular gene – entries begin with the word LOCUS and end with //, and contain the gene name at some point between. I understand that I can use grep’s flags -A
, -B
, and -C
to print n lines after/before a string match, but the actual entries are variable in length. How would I use grep to search for my string (gene name), and then print all the lines before the match up to and including a line beginning with "LOCUS", and all lines up to and including a line indicating the end of the entry, which is just "//"?
I'm open to all suggestions – is there a way to have the -A
and -B
flags match a strings ("LOCUS" and "//") or something to that effect? Should I be using awk instead?
Edit: This is a simplified input example – each record begins with "LOCUS" and ends with "//". This example contains three records:
LOCUS scaffold1|size100
/gene="gene1"
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
//
LOCUS scaffold99|size
/gene="gene2"
CGTTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
//
LOCUS scaffold199|size1000
/gene="gene3"
AGTTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
AGTTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
AGTTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
//
I would like to search for "gene2" and print the text from the first instance of "LOCUS" before the match through the first in "//" after the match. Ideally, I would like the following output:
LOCUS scaffold99|size
/gene="gene2"
CGTTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
GATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACAGATTACA
//
//
instead of//
...