Raw Input Desired Output
1 cat chicken
2 bird dog apple
3 dog orange cat
4 cat juice coffee
5 cow milk dog
2 bird dog apple
5 cow milk dog
Script and Comments
Script1
[ 1] /PAT1/!d
[ 2] /PAT2/d
Comments
  1. Any line without PAT1 will be discarded in [1]. In this case sed will start next cycle.
  2. Lines survived from [1] must contain PAT1. If they contain PAT2, they will be discarded in [2].
  3. After [2], sed will print the survived line and start next cycle.
  4. [Note] Command 'd' will cause sed to clear the pattern space without printing anything, then start next cycle.
Script2
[ 1] /PAT2/d
[ 2] /PAT1/!d