Raw Input Desired Output
Line 1
Line 2 PAT
Line 3
Line 4 PAT
Line 5 PAT
Line 6
ODD
Raw Input Desired Output
Line 1
Line 2 PAT
Line 3 PAT

  
Script and Comments
Script1
[ 1] /PAT/H
[ 2] $!d
[ 3] x
[ 4] s/^((\n[^\n]*){2})*\n[^\n]*$/ODD/p
[ 5] d
Comments
  1. The `-r' option of GNU sed must be used or we have to escape parentheses and braces.
  2. `Pattern Space' and `Hold Space' are abbreviated to `PS' and `HS', respectively.
  3. This script will keep every line containing PAT in the HS and count the number of lines after reading the last line.
  4. Step [1] is used to append every line containing PAT to the HS.
  5. After reading the last line, Step [3] exchanges the contents of PS and HS.
  6. Step [4] will replace the PS with `ODD' and then print it if there are odd number of lines in the PS.
Script2
[ 1] /PAT/s/^.*/\n/
[ 2] /\n/H
[ 3] $!d
[ 4] x
[ 5] s/^(\n{4})*\n{2}$/ODD/p
[ 6] d
Comments
  1. The previous script will consume too much memory for huge files with many lines containing `PAT'.
  2. Instead of copy entire line to the HS,this script appends to the HS a newline character `\n' each time a line containing `PAT' is read.
  3. Step [1] replaces the entire line (PS) with `\n' if it contains PAT, and the `\n' will be appended to HS in Step [2].