Results 1 to 4 of 4
I need to substitute the value of a variable with the value of another variable
I did a test
# cat file
this is needee
# cp file file.old
# ...
- 03-02-2009 #1Linux Newbie
- Join Date
- Feb 2007
- Posts
- 248
problem in substituting the values of variables via 'sed'
I need to substitute the value of a variable with the value of another variable
I did a test
# cat file
this is needee
# cp file file.old
# echo $STRING1
this is needee
# echo $STRING2
this is NOT needee
# cat file.old | sed 's/$STRING1/$STRING2/' > file
# cat file
this is needee
i.e no change in the 'file'
please help me in substituting the values of variables via 'sed'
- 03-02-2009 #2Just Joined!
- Join Date
- Mar 2009
- Posts
- 5
# cat file.old | sed "s/$STRING1/$STRING2/" > file
replace ' with " for variables
- 03-03-2009 #3Linux Newbie
- Join Date
- Feb 2007
- Posts
- 248
Hi thanks stokilo, yes double quotes works

another relevant question
the following code has to do two task
1, if there is no TIMEZONE set, then set it in /etc/sysconfig/clock
2, if there is wrong TIMEZONE in /etc/sysconfig/clock, then fix it
ok, now
the proper value of timezone in /etc/sysconfig/clock should be
TIMEZONE="Asia/Tashkent"
right now the value of timezone(wrong) in /etc/sysconfig/clock is
TIMEZONE="aa/tshknt"
#!/bin/bash
CLOCK=/etc/sysconfig/clock
TIMEZONE_STRING='TIMEZONE="Asia/Tashkent"'
CHECK_TIMEZONE=$(grep ^TIMEZONE $CLOCK)
if [ -z "$CHECK_TIMEZONE" ]; then echo "$TIMEZONE_STRING" >> $CLOCK
else cp "$CLOCK" /tmp/clock
cat /tmp/clock | sed "s/$CHECK_TIMEZONE/$TIMEZONE_STRING/" > $CLOCK
fi
exit
upon execution of the above code, I got the following error
sed: -e expression #1, char 36: unknown option to `s'
- 03-03-2009 #4Just Joined!
- Join Date
- Mar 2009
- Posts
- 5
sed -e "s/$CHECK_TIMEZONE/$TIMEZONE_STRING/"
Did you forget -e option ? I didn't check this script but try with -e switch.
Best regards
Slawek


Reply With Quote