I have a text file and I want to count the number of times a line has specific text. The first line of text is written on 5 columns and the other line of text on 6 columns:

The 5-column line:

***** No hits found *****    

the 6-column line:

Sequences producing significant alignments: (Bits) Value    

I would also like to extract information the positive hits for the 6-column line. That is, on a given line where the text above is found (Sequences producing..), I want to extract the text from two lines below (~10 columns) and 6 lines above (9 columns) this line. The line 6 lines above the positive hit (i.e. (Sequences producing..) begins with


Although the line 2 lines below the hit varies.

First answer:

grep -c "string" infile    

The second question is a bit more complicated

closed as unclear what you're asking by αғsнιη, Stephen Rauch, countermode, Anthon, Scott Jul 28 '17 at 7:08

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Have you looked at the -A and -B flags for grep? They grab context lines around your match. It looks like your second answer is grep -B6 -A2 'Sequences producing' – Jeremy Dover May 10 '17 at 21:13
  • 6
    It would probably help if you showed an example of your input file. I assume you're parsing BLAST output, so please show us an example and the output you would want to see from that example. – terdon May 10 '17 at 21:14
  • 1
    Always remember that a picture is worth a 1000 words. So an input/output sample would have been the way to go here. – user218374 May 11 '17 at 1:54
  • 1
    Do what Jeremy said regarding the -A and -B flags for grep: grep -A2 -B6 'Sequences producing' filename and either pipe it through nl for line numbers or wc -l for amount of lines. grep -A2 -B6 'Sequences producing' $filename | wc -l will show number of 'hits' grep -A2 -B6 'Sequences producing' $filename | nl will show each "hit" with a line number in front of it. – Patrick May 11 '17 at 2:43