problem with sed command in shell script.

Guys,
I've a problem in the "sed" command used in my shellscripts

This is the problamatic line in my shell script:
sed -e 's/${line1}/${line1_m}/g' prod_hier_1234.txt > test.txt
It doesn't do the job of replacing the string stored in variable 'line1' to 'line1_m'.

However If I replace the line as below(by replacing the actual string)
sed -e 's/A,P,CMD,20-SEP-06,2006/A,P,CMD,20-SEP-06,20062814/g' prod_hier_1234.txt>test.txt
it works..

Could someone provide thougs about what could be the problem and the possible solution?

Thanks,
Bhagat

Hi,

I haven't tested it but don'y you have to double quote your sed scripting instead of single when using variables?
sed -e "s/${line1}/${line1_m}/g" prod_hier_1234.txt > test.txt

With
sed -e "s/${line1}/${line1_m}/g" prod_hier_1234.txt > test.txt
.....
It throws the error message "sed: Function s/ cannot be parsed."..
Oooops..

try the follwing

line1="abc"
line2="efg"
echo "sed -e 's/$line1/$line2/g' prod_hier_1234.txt > test.txt" > temp.sh
sh temp.sh
rm temp.sh

hope this works :stuck_out_tongue:

sed -e 's/'${line1}'/'${line1_m}'/g' prod_hier_1234.txt > test.txt

error given:-

$ sed -e 's/'${line1}'/'${line1_m}'/g' prod_hier_1234.txt > test.txt
sed: -e expression #1, char 0: no previous regular expression

May be you have a expression which consists of many words. Try enclosing the entire expression in quotes and see. But I am not sure of this.

What contains your variables line1 and line1_m ?

If line1 contains special chars for sed like [ . *, they must be preceded by \ to loose they special meaning.
For example if you want to replace the string 'Bye.', line1 must contains 'Bye\.'

In the same way the replacement string can contains special char or expression like & \1. Using \ may bee need.
For example to replace string 'You' by 'You & Me', line1_m must contains 'You \& Me'.

Jean-Pierre.

line1:
A,P,CMD,21-SEP-06,2006
and line1_m:
A,P,CMD,21-SEP-06,2006,2814

Guidance plz.

sed -e 's/'${line1}'/'${line1_m}'/g' will work in Bash not sh what shell are you using

I'm using sh.