Raw Input Desired Output
1 cat chicken
2 bird dog apple
3 dog orange cat
4 cat juice coffee
5 cow milk dog
1 cat chicken
2 bird DOG apple
3 dog orange cat
4 cat juice coffee
5 cow milk dog
Script and Comments
Script1
[ 1] /PAT1/{
[ 2] s/PAT1/PAT2/
[ 3] :loop
[ 4] n
[ 5] b loop
[ 6] }
Comments
  1. Flowchart:
  2. When sed meets the line containing the FIRST occurrence of PAT1, step [2] will replace the ony the first one with PAT2 since option 'g' is NOT specified; otherwise, all the occurrences in the line will be replaced.
  3. Now, all sed has to do is just reading next line and then print it out. Loop consists of Step [3] thru [5] will do this.