0

I have a text file. It contains some line like this:-

I don't want to see this line 1 and next line.
exception: 14
I want to see this line 1 and next line.
exception: 16
I don't want to see this line 2 and next line.
exception: 13
I want to see this line 2 and next line.
exception: 19

I want the output like this:-

I want to see this line 1 and next line.
exception: 16
I want to see this line 2 and next line.
exception 19

I tried to use awk to solve it. But cannot get the right result. My awk script:

$ cat test.awk  
awk '
  {if ( $2 > 14 )  print prev $0; next}
' test.txt

Result:

$ ./test.awk
I don't want to see this line 1 and next line.
I want to see this line 1 and next line.
exception: 16
I don't want to see this line 2 and next line.
I want to see this line 2 and next line.
exception: 19
Bin
  • 1
  • 2

1 Answers1

0

If you want to use for this, then you are missing something small, you never define prev:

awk '/^exception:/ && ($NF > 14) {print prev; print $0} {prev = $0}' file

You also should say that your line should start with exception: otherwise, you might have problems when you have a line like

exception: 2
Some 15 years ago I had an exception
exception: 1

This would print something wrong.

Otherwise, use (See How do you grep a file and get the next 5 lines)

kvantour
  • 25,269
  • 4
  • 47
  • 72