Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this

INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />'
GREP_FOR='</JOB>'
TMP_FILE=/tmp/lineArray.$$
if [[ -e ${DRF_PATH} ]]; then
 continue
else
 echo "File ${DRF_PATH} does not exist."
 exit 1
fi
grep -n "${GREP_FOR}" ${DRF_PATH} | cut -d: -f1 > ${TMP_FILE}
cat ${TMP_FILE} | while read LINE
do
 set -A LINE_ARRAY ${LINE_ARRAY
[*]} $LINE
done

((COUNT=0))
while (( ${COUNT} < ${#LINE_ARRAY
[*]} ))
do
 INSERT_LINE=${LINE_ARRAY[${COUNT}]}
 ((INSERT_LINE=${INSERT_LINE}-1))
 echo "[Insert]  ${INSERT_NODE} at line ${INSERT_LINE}"
 ((COUNT=${COUNT}+1))
done
exit 0

And I have the info I need now, except the actual insert part
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 75
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 149
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 228
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 300
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 364
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 428
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 518
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 569
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 644
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 709
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 782
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 861
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 912
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 985
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 1058
...

---------- Post updated at 07:00 PM ---------- Previous update was at 06:55 PM ----------

I was reading up on awk, but now it looks like maybe I should use sed. Still reading the man file though.

---------- Post updated at 07:20 PM ---------- Previous update was at 07:00 PM ----------

Those didnt help, looking at ed now.

Yes, with "sed" its very easy.
You have given the output you have now.
Give the output that you want it to be.

sed wont work its not a replace

The text example would look like

75:    </ON>
76:   </JOB>
77:   <JOB
78:    APR="1"

So I have my new insert string
<QUANTITATIVE NAME="DB-BLODS" QUANT="1" />

And I need to stick that in after line 75

Wow.... you learnt all about sed in 10 minutes.
That was pretty fast.

Again, if you want a solution, do this.
post what you have and what you need.
Example:
I get:

 
aaa
bbb
ccc
ddd

I want it as follows:

 
aaa
bbb
11111111
ccc
ddd

11111111 must come after bbb
Or may be
11111111 must come before ccc

My problem with sed, and its my inexperience with the command is sed syntax

sed '/abc/123/' <file name>

All the xml tags and inserts have their own / part of the xml syntax.

So I'm thinking something like

 ed - ${DRF_PATH} << EOF
 ${INSERT_LINE}
 ${INSERT_NODE}
 w
 q
 EOF

But I havent tested it.

---------- Post updated at 09:11 PM ---------- Previous update was at 08:23 PM ----------

((COUNT=0))
while (( ${COUNT} < ${#LINE_ARRAY
[*]} ))
do
 INSERT_LINE=${LINE_ARRAY[${COUNT}]}
 ((INSERT_LINE=${INSERT_LINE}-1))
 echo "[Insert]  ${INSERT_NODE} at line ${INSERT_LINE}"
 ed - ${DRF_PATH} << EOF
 ${INSERT_LINE}a
 ${INSERT_NODE}
 .
 w
 q
 EOF
 ((COUNT=${COUNT}+1))
done
exit 0

Ok that does what I want, but works manually but not in the script.

$ ed - ../BIL-EXF.DRF.xml << EOF
> 75a
> <QUANTITATIVE NAME="DB-ODSAL" QUANT="1" />
> .
> w
> q
> EOF

Makes the insert I need at the right spot, but when run in the script it failes.

59: ed - ${DRF_PATH} << EOF

insert="<xxx>XML to insert</xxx>"
awk -v insert="$insert" '
  /<\/whatever>/ { print insert }
                { print }
   ' FILENAME

Thanks cfajohnson and edidataguy.

I did finally get it to work with sed, thanks.

GREP_FOR='<\/JOB>'

sed '
/'${GREP_FOR}'/ i\
<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" \/>
' ${DRF_PATH} > ${DRF_PATH}.tmp
mv ${DRF_PATH}.tmp ${DRF_PATH}

Glad you were able to solve it by yourself.
Not only can you insert, you can append and change also.

Also, there is a way you can avoid the last line (the move command).
Do a little more research on "w" option. Of course, it has its own limitations. But if the file size is really big, then you save a lot of time and resources.