I have a text file with many thousands of lines, a small sample of which looks like this:
InputFile:
PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0
PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 1 1 -1 -1 -1 -1 0 501 0
PS002,003 XNQ 0 2 -1 -1 -1 5 -1 -1 -1 3 2 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS002,003 HWN= 0 7 -1 -1 -1 -1 -1 -1 3 3 2 -1 7 2 2 -1 -1 -1 0 503 0
* 0 -1 512 1 411 0 0 .q 4 LineNr 5 ClauseNr 1: 1: 3: 131: 0 0 SentenceNr 3 TxtType: ?Q Pargr: 12 ClType:xYq0
* 0 -2 111 1 411 0 0 .. 3 LineNr 10 ClauseNr 1: 1: 4: 131: 0 0 SentenceNr 6 TxtType: ? Pargr: 1 ClType:xYq0
PS002,005 W 0 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0
PS002,005 B 0 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 0 -1 -1 -1 -1 -1 -1 -1
PS002,005 XM> 0 2 -1 -1 -1 11 -1 -1 -1 1 1 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS002,005 H 0 7 -1 -1 -1 -1 -1 -1 3 1 2 -1 7 5 2 -1 -1 -1 0 505 0
PS002,005 DLX 0 1 5 18 1 0 -1 1 3 1 2 -1 1 1 -1 -1 -1 -1 0 501 0
PS002,005 >NWN 0 7 -1 -1 -1 -1 -1 -1 3 3 2 -1 7 7 2 -1 -1 -1 0 503 0
PS012,004 >BD 0 1 5 15 1 0 -1 1 3 1 2 -1 1 1 -1 -1 -1 -1 0 501 0
PS012,004 MRJ> 0 3 -1 -1 -1 1 -1 -1 -1 0 0 2 3 3 2 -1 -1 -1 0 502 0
PS012,004 KL 0 2 -1 -1 -1 1 -1 -1 -1 0 0 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS012,004 HJN 0 7 -1 -1 -1 -1 -1 -1 3 3 1 -1 7 2 2 -1 -1 -1 0 503 0
PS012,004 SP> 0 2 -1 -1 -1 12 -1 -1 -1 3 1 3 2 0 -1 -1 -1 -1 -1 -1 -1
PS012,004 PLG 0 1 6 18 1 12 -1 62 -1 3 1 3 13 -2 2 -1 -1 -1 -11 500 0
What I would like to do is that if a given line meets the conditions $16=="0" && $22=="-1" and the immediately following line has $22=="503" && $4=="7" && $16=="2" then print every set of these two consecutive lines.
Desired Output:
PS002,003 XNQ 0 2 -1 -1 -1 5 -1 -1 -1 3 2 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS002,003 HWN= 0 7 -1 -1 -1 -1 -1 -1 3 3 2 -1 7 2 2 -1 -1 -1 0 503 0
PS012,004 KL 0 2 -1 -1 -1 1 -1 -1 -1 0 0 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS012,004 HJN 0 7 -1 -1 -1 -1 -1 -1 3 3 1 -1 7 2 2 -1 -1 -1 0 503 0
Thus far I have tried various revisions of the following awk code which has gotten me fairly close:
awk '$16=="0" && $22=="-1"{f=$0; f++; next} $22=="503" && $4=="7"{n=$0} {print f"\n"n}' InputFile
Nevertheless, I continue to not be able to figure out how to get this to work. I would very much appreciate any help to get this one-liner to work as desired. Thanks!