sed substitution

Hi I am trying to do a text insertion in a text file at a particular line number in a shell script. However its not working.

sed '122i\
> for j in \`echo $MyList\` ; do perl -pi -e\'s#01\/01\/2009#01\/01\/2011#\' $j ; done' $HOME/MyScript.ksh

The Actual line to be inserted at line 122 is:

for j in `echo $My_List` ; do perl -pi -e's#01/01/2009#01/01/2011#' $j ; done

Why is it not doing so?
Please tell me how to do it.

You need an input file and output file for sed once you validate the output file you can overwrite the original file. The line you want to add contains single-quotes and even though you are escaping them you have to use double-quotes around the sed command. The last thing is you need to escape the '$' in front of the last 'j' if you want it to print literally in the file.

sed "122i for j in \`echo $MyList\` ; do perl -pi -e\'s#01\/01\/2009#01\/01\/2011#\' \$j ; done" $HOME/MyScript.ksh > output file

Its not working :frowning:

# cat of output file
118 This is a test file
119 This is a test file
120 This is a test file
121 This is a test file
122 for j in `echo ` ; do perl -pi -e's#01/01/2009#01/01/2011#' $j ; done
123 This is a test file
124 This is a test file
125 This is a test file
126 This is a test file
127 This is a test file

works for me using my inputFile

sed "122i for j in \`echo $MyList\` ; do perl -pi -e\'s#01\/01\/2009#01\/01\/2011#\' \$j ; done" inputFile > output

But where is $MyList on line 122?
I am using Solaris, is this command OS dependent?

escape the "$" in front of Mylist "\$Mylist" so it is not resolved as a variable.