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
sed "/\"rep\"/a \"rep\"x" sample.txt
break
fi
done <out.txt
---------------------------
Sample.txt
line1
line2
line3
line4
line5
line2
line3
line3
line4