I have tried every possible solution available on the Stack Overflow and its related websites but did not find any solution. I spent a reasonable amount of time on this issue and am finally posting this question.
I want to use the sed
command with shell variables. My script is pretty simple:
## string to replace is the text after comma in the variable pc.
to_replace=${pc#*,}
echo $to_replace
##text to be replaced by the following line
replace_with="PARTITIONED BY ($pc1);"
echo $replace_with
## use sed command to replace.
sed "s@$to_replace@$replace_with@" $entry ## $entry is the variable that contains the file name
The two echo
commands give the following outputs respectively:
PARTITIONEDED BY (date_key ); ## the text I want to be replaced
PARTITIONED BY ( date_key int ); ## the text I want to replace with
I either get an error:
sed: -e expression #1, char 2: unterminated `s' command
or the text does not get replaced at all.
Someone please help. I am using Centos 6 (if that matters). Thanks in advance!
sed
issue, but strange things happening du to shell magic. Maybe the output ofecho "s@$to_replace@$replace_with@"
can reveil what scriptsed
has to deal with.sed s@ PARTITIONEDEDED BY (date_key );@PARTITIONED BY ( date_key int );@@ <file_name>
@
at the end come from?