Q:

Using sed in a script

I am trying to insert a line of text under a line of existing text in a file as per the following excerpt:
#! /bin/bash

MATCH='Text_To_Look_For'
INSERT='Text_To_Add'
FILE='/home/user/test.txt'

echo "Modifying file..."
sed -i 's/"$MATCH/$MATCH"\n"$INSERT"/' $FILE

echo "Done." 

I have tried all sorts of combinations of / ' and " but cant seem to  make it work.  The script will run now but not insert anything.  In other formats I keep getting errors relating to:

unterminated s command
  sed: -e expression #1, char 35: unknown option to `s'

A:

You didn't say what version of sed you're using, I've seen something similar on MacOS, where the standard sed is rather poor... Worked around by installing a GNU Sed (invoked later as gsed). 
Anyway, can you give a try with sed a command (which stands for append):
#! /bin/bash

MATCH='Text_To_Look_For'
INSERT='Text_To_Add'
FILE='/tmp/xxx'

echo "Modifying file..."
sed -i "/$MATCH/a $INSERT" $FILE

echo "Done." 

Best regards,
Jarek

