insert line into file using variable

Hi all, I am trying to insert couple of lines before first occurance of line3 which occuring after line 5.
So I identified the line 5 line number in the file. Also redirected the all line3 line number to out.txt.
Now I have problem in inserting the line using the variable $rep. Please help me with awk or sed. Or anyother solution.

lno=$(grep -nw "line5" sample.txt | cut -d: -f1)
grep -n "line3" sample.txt | cut -d: -f1 >out.txt
while read s1
do
if [ $s1 -gt $lno ]; then
rep=$s1 - 1
# awk 'NR == $rep{print "new line"}1' sample.txt >outfile :eek:
sed "/\"rep\"/a \"rep\"x" sample.txt :confused:
break
fi
done <out.txt
---------------------------
Sample.txt
line1
line2
line3
line4
line5
line2
line3
line3
line4

do you mean like this???

sed '/line5/{
n
/line3/d
a\
new line inserted
}' filename

line1
line2
line3
line4
line5
line2
new line inserted
line3
line3
line4

exactly Vidhayadhar. Thanks
My actual file also has / operator in the inline text as well as in the replacement text. How I can integrate the /. After function x before </head> and </script>

example:
after function (x)

line inserted

</script>

</head>