0

I know this question has the answer to getting 5 lines after pattern. I am trying my own method with this command:

awk 'BEGIN{count=0} { /pattern/{count=5;next}; if(count>0){print; --count;} }' inputFile

but this gives me syntax error.

  • 1
    `/pattern/` can't be used like this inside of curly braces. You can replace it with `if ($0 ~ /pattern/)`. – Benjamin W. May 04 '18 at 15:48
  • This should do what you want : `awk '/pattern/{count=5;next}(count>0){print; --count}` – kvantour May 04 '18 at 16:08
  • See https://stackoverflow.com/a/17914105/1745001 for how to really do this. – Ed Morton May 04 '18 at 16:50
  • 2
    @BenjaminW. right but you can just write `if (/pattern/)`, no need to add`$0 ~` as that's the default. Also, the OP doesn't need any `if` statements at all - just get rid of the surrounding `{` and `}` to use awks condition/action syntax. – Ed Morton May 04 '18 at 16:51

0 Answers0