Sed issue in K Shell programming

I am doing the following script in k shell

sed -i 's/FILENAME/$i/g' TEST/test$j.ctl > TEST/control$j.ctl

In the file it replaces $i for all FILENAME, it doesnot replace with the value of i. I put single quotes like below

sed -i 's/FILENAME/'$i'/g' TEST/test$j.ctl > TEST/control$j.ctl

I get following error

sed: -e expression #1, char 13: Unknown option to `s'

I did following

sed 's/FILENAME/'$i'/g' TEST/test$j.ctl > TEST/control$j.ctl
same error and it created 0 bytes control$j.ctl

Please help..

Simply use double quotes instead of single quotes:

sed -i "s/FILENAME/$i/g" TEST/test$j.ctl > TEST/control$j.ctl

Single quotes prevents variable expansion.

Nope same error

sed: -e expression #1, char 14: Unknown option to `s'

sed -i "s/FILENAME/$i/g" TEST/test$j.ctl > TEST/control$j.ctl

sed -i "s/FILENAME/'$i'/g" TEST/test$j.ctl > TEST/control$j.ctl

Tried both but same error

using #!/bin/ksh shell

Oh sorry. Now I don't have ksh available for testing and, to be honest, I don't use it often and don't know it very well.
But it seems strange to me that a basic variable expansion doesn't work in ksh like in bash or sh :confused:

Hi,

Please check the following syntax and let me know it is correct or not

sed 's/FILENAME/'${i}'/ig' filename >filename1

or

sed "s/FILENAME/'${i}'/ig" TEST/test$j.ctl > TEST/control$j.ctl

robotronic's answer works.
Is that the only line you have in the script?

nope it did not work

I am posting the script

#!/bin/ksh
j=1
for i in `ls /TEST/DIR1/test_dat?`;
do
echo $i
echo $j

sed -e 's/FILENAME/${i}/ig' TEST/test$j.ctl > TEST/control$j.ctl
let j=$j+1
done

When I run this script.

I get following error

/TEST/DIR1/test_dat1
1
sed: -e expression #1, char 13: Unknown option to `s'
/TEST/DIR1/test_dat2
2
sed: -e expression #1, char 13: Unknown option to `s'
/TEST/DIR1/test_dat3
3
sed: -e expression #1, char 13: Unknown option to `s'
/TEST/DIR1/test_dat4
4
sed: -e expression #1, char 13: Unknown option to `s'

Hi,

sed "s/FILENAME/$i/g" TEST/test$j.ctl > TEST/control$j.ctl

Hope the above helps you..

Regards,
Chella

What's that i before the g?
Where are the double quotes?