How to insert text in the middle of a file?

Hi,

So far i've made a script that takes two argument, 1st is the contents and the 2nd is the named file. At the moment i've managed to insert new contents as a new line at the top, but i want to ask how can you insert contents in the middle of the file?

Source Code

#!/bin/bash
#Write content to exisiting file
if [ $# -ne 2 ]
then
echo "run the script again and insert two values please"
exit 0
fi
{
  printf "%s\n" "$1"
  cat "$2"
} > tempfile.$$
mv tempfile.$$ "$2"

Many thanks

Some ways:

awk -v n=$(wc -l < file) 'NR==int(n/2)+1{system("cat contentfile")}{print}' file

or:

n=$(($(wc -l < file)/2)); sed "$n r contentfile" file
# sed "$(($(sed -n '$=' infile)/2)) r addfile" infile
$ ruby -e 'BEGIN{a=File.read("file").split("\n");s=a.size} ;;END{ a.insert(s/2,"new");puts a.join("\n") }  '