Raw Input
Line  1       Do not print this
Line  2       Print this
Line  3 YES   Do not print this
Line  4       Print this
Line  5 YES   Do not print this
Line  6
Line  7
Line  8       Print this
Line  9 YES   Print this, because Line 10 contains YES
Line 10 YES   Do not print this
Desired Output
Line  2       Print this
Line  4       Print this
Line  8       Print this
Line  9 YES   Print this, because Line 10 contains YES
Script and Comments
Script1
[ 1] $!N
[ 2] /\n.*PAT/P
[ 3] D
Comments
  1. 'Pattern space' is abbreviated to 'PS'.
  2. When sed reads in a line other than the last one, step [1] will append next line. Now PS has two lines.
  3. In Step [2], if the second line of PS contains PAT, command 'P' will print the first one.
  4. In Step [3], Command 'D' will cause sed to delete the first line of PS, then jumps to Step [1]; if PS contains only one line before execution of command 'D' (note this should be the last line of datafile), 'D' will just delete it.