[Solved] Replace first occurrence after match

hey guys, i have been trying to work this thing out with sed with no luck :confused:
i m looking for a way to replace only the first occurrence after a match
for example :

 
Cat
Realized what you gotta do
Dog
Realized what you gotta do
Sheep
Realized what you gotta do
Wolf
Realized what you gotta do

i would like to replace the first occurrence of "you" to "us" only if the word "Sheep" appeared somewhere erailer (could be a random number of lines befor or as a part of a sentence).
so results for this thing will be :

 
Cat
Realized what you gotta do
Dog
Realized what you gotta do
Sheep
Realized what us gotta do
Wolf
Realized what you gotta do

any help will be appreciated
10x

Try:

perl -0pe 's/(?<=Sheep)(.*?)you/$1us/s' file

Try:

sed '/Sheep/,/you/s/you/us/' file

the perl script worked perfectly, the sed script replaced all the match, and not only the first occurrence.

Hello,

Following may also help.

awk '/Sheep/ && a!=1 {print;getline; sub(/you/,"us");a=1}1'  file_name

Output will be as follows.

Realized what you gotta do
Dog
Realized what you gotta do
Sheep
Realized what us gotta do
Wolf
Realized what you gotta do
Sheep
Realized what you gotta do

Thanks,
R. Singh

Hi ,
Thanks all for your reply ..

I have the similar query but for my case I want add another line before that matched line (ie, Realized what you gotta do).

thanks in advance

Some more awk

$ awk '!f && s{sub(old,new);f=1}/Sheep/{s=1}1' old="you" new="us" file

--edit---

$ awk '!f && s{print newline;f=1} /Sheep/{s=1}1' newline="its my new line" file
Realized what you gotta do
Dog
Realized what you gotta do
Sheep
its my new line
Realized what you gotta do
Wolf
Realized what you gotta do