0

I need to view 5 line before grep output.
I used the command,

grep -B 5 pattern filename

But was thrown with the error /usr/bin/grep: illegal option --B

How to handle this situation and to view 5 lines before grep statement?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Sadhun
  • 264
  • 5
  • 14
  • possible duplicate of [grep a file, but show several surrounding lines?](http://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-lines) – Elwinar Jun 05 '14 at 10:55
  • 1
    Sounds like your grep doesn't support that option. – Pandu Jun 05 '14 at 10:56
  • possible duplicate of [print specific number of lines after matching pattern](http://stackoverflow.com/questions/17283567/print-specific-number-of-lines-after-matching-pattern) To get the `-B` behaviour, just use `tac` before (print in reverse) and `tac` again once done. – fedorqui Jun 05 '14 at 11:17
  • You could install a different version of grep, or you could install [ack](http://beyondgrep.com) which is a greplike tool that supports -A, -B and -C just fine. – Andy Lester Jun 06 '14 at 04:06

2 Answers2

0

Have you considered using awk?

If it is a viable option, check this one, long, line:

$ awk '/PATTERN/{for(i=1;i<=x;)print a[i++];print}{for(i=1;i<x;i++)a[i]=a[i+1];a[x]=$0;}' x=5 file

The above command will return the five lines before the matched pattern and the matched pattern (i.e. 6 lines in total whereas grep -B 5 returns five lines in total including the matched one).

x designates the number of lines

I hope this helps.

Simply_me
  • 2,840
  • 4
  • 19
  • 27
  • Thank You Simply me:) This works. I would like to understand the concept hidden in it. I understod the first for loop, but the second for loop, I couldnt get hold of it. Can you please explain the concept in it? {for(i=1;i – Sadhun Jun 09 '14 at 06:32
  • @Sadhun Great! you can refer to `awk` for loop user guide [link](https://www.gnu.org/software/gawk/manual/html_node/For-Statement.html) , simply put `a` is the array that stores all of the elements, the first loop prints, and for the second loop I'll give you a hint, try adding `print a[i+1]` after `a[x]=$0` and play around with the loop. Should be a learning experience as well :-) Lastly, if you can choose my answer as the answer for this question it'll be great. – Simply_me Jun 09 '14 at 18:54
0

Simply_Me's answer is correct but copies all previous lines on each non-match. (nit picking technicality: even if gawk's implementation avoids copying the strings themselves it still would have to re-hash the index). So, still sticking with awk:

#!/usr/bin/awk -f

BEGIN { cl=1; }

/foo5/ {
    for (i=cl; i<cl+N; i++)
    print pLines[i % N];
    cl=1;
}

{  pLines[cl % N] = $0; cl = cl + 1; }

cl stands for current line. If we have a match we print N lines we stored in pLines array before. You may wish to print the pattern itself, which I didnt. Regardless of seeing the pattern, we store the line we just processed in pLines with the % operator, so we do not have to shift the array.

For

zoo0
blah1
foo2
bar3
hehe4
lisp5
foo5
blah6
foo7
bar8
hehe9
foo5

This prints

blah1
foo2
bar3
hehe4
lisp5
foo5
blah6
foo7
bar8
hehe9

which is the previous 5 lines not including the pattern.

It also works reasonably on

hehe4
lisp5
foo5
blah6
foo7
bar8
hehe9
foo5

where there is no 5 previous line before the first match of foo5. In this case it prints empty lines. You may need to tailor that for your needs.

user1666959
  • 1,805
  • 12
  • 11
  • I'm not sure what you mean. I tested the script several times, and it does print 5 lines before the matched pattern. Pls double check your script when running it. `$ grep -B 5 mango 5before.txt apples oranges grapes mango orcl msft nugt dust mango` `$ awk '/mango/{for(i=1;i<=x;)print a[i++];print}{for(i=1;i – Simply_me Jun 06 '14 at 20:07
  • I apologise for misreading the solution and will edit the corresponding comment. I saw the pattern and thought everything afterwards belongs to it. Nevertheless, it shifts the array meaning on every non-match it moves the element at index N to index N-1: gawk's hash table (or is it a balanced tree?) implementation of arrays will not like that once you increase N to a few hundred or start moving around very long lines. BTW, I didn't down vote your solution, just consider mine nicer :). – user1666959 Jun 06 '14 at 20:23
  • thank you for editing the original comment and the info regarding the hash table. – Simply_me Jun 06 '14 at 22:15