Add newline before another line of occurance

Hi ,

I have a file like

I want to add a new line before "Realized" only when it comes after "Sheep".
There may be any line betwwen "Sheep" and "Realized" other than this two.
Say my new line is "a goat", so the desired result would be

Can any body help me in this?

thanks

awk '
/Sheep/ {found=1}
found==1 && /Realized/ {print "new line";
found=0}
{print}
' file
$ awk '/Sheep/{F=1} /Realized/{if(F){print "a goat"}else{F=""}} {print}' file
Cat
Realized
Dog
Realized
Sheep
s
d
f
jk
a goat
Realized
Wolf
a goat
Realized
Sheep
s
d
a goat
Realized

Hi "MadeInGermany",
Thanks for your reply. But can you please explain a bit. I am very new in this.

Thanks again..

---------- Post updated at 01:08 PM ---------- Previous update was at 01:03 PM ----------

Hi Pamu,

Its printing new line before every "Realized" once it found "Sheep".
But newline "a goat" is not required between "WOLF" and "Realized".

Though thanks for you reply.

This answer might help you

[Solved] Replace first occurrence after match by Akshay Hegde - UNIX for Dummies Questions & Answers - Unix Linux Forums

An explanation of my previous post follows.
The shell runs the awk command and passes a (multi-line) argument in ticks, and another argument (file). (The ticks are also called single quotes.)
The awk sees two statements in the 1st argument.
awk statements are usually in curly brackets. Here they are prefixed with a condition (implicit if clause).
Multiple statements in a curly bracket block are separated by a semicolon - or are on separate lines.
awk runs the statements for each line of its input file(s).
The logic is simple here:
if Sheep is found in the current input line, store 1 in a variable (count). Note that initially a variable is empty (0 in number context).
If found is equal to 1 (because was set to 1 in this or a previous input line) and Realized is found in the current input line then print the new line; also reset found to 0 so if Realized is met again it won't print another new line.
Then (I just added it to my previous post) print the input line.

Thanks MadeInGermany,

It works fine for me.

thanks a lot.

Hi MadeInGermany,
I need another help.

If I want to add the line after "Realized" then what to do.

Thanks

{print} the line first, before the conditional {print "new line"} :

awk '
{print}
/Sheep/ {found=1}
found==1 && /Realized/ {print "new line"; found=0}
' file

Thanks MadeInGermany.
It works..

---------- Post updated at 05:20 PM ---------- Previous update was at 05:13 PM ----------

Hi,
Now I have long list of files. few of those having these match.
i.e. Sheep then Realized.

first I have to seperate those files and then replace.
how to replace I got (thanks to MadeInGermany)
Is there any way to get those files name.

Can you help?

Thanks

Reset the found variable at the beginning of a new file, and at a match print the FILENAME.

awk '
FNR==1 {found=0}
/Sheep/ {found=1}
found==1 && /Realized/ {print FILENAME; found=0}
' files...