awk to replace second occurance

#original file
.
.
~
~
Index=2  
xxx
replace  #dont replace 1st occurance
yyy
Index=2
xxx
replace  #substitue replace with "REPLACE"
yyy
Index=2
xxx
replace #substitue replace with "REPLACE"
yyy
Index=3
xxx
replace 
yyy

Expected output file:

.
.
~
~
Index=2
xxx
replace  
yyy
Index=2
xxx
REPLACE  
yyy
Index=2
xxx
REPLACE  
yyy
Index=3
xxx
replace 
yyy

Hi,

This is the simiplified file I am working on.. I need to :

  1. search for 2nd occurance of a string pattern ("Index=2"in file)
  2. Go forward 2 lines after this
  3. Replace current line with new pattern (ex: "REPLACE" )

I am using awk for checking the 2nd oocurance, but dont know
how to step 2 lines forward and replace the string.

#awk.sh

/Index=2/{
   mcount++
   if(mcount == 2){
      {print NR+2}
      #need to substitute NR+2
   }
}
awk '/^replace/ {c++}; (c==2)||(c==3) {$1=toupper($1)}1' urfile 

Try this:

awk '/Index=2/{print;getline;print;getline;if (cnt == 0)cnt+=1; else { gsub("replace","REPLACE")}}1' filename

cheers,
Devaraj Takhellambam

Thanks guys...both the solutions solves my problem..