-1

I have a script that restores db dump. As a parameter a database name is passed to this script. In a dump there is a following line:

ALTER DATABASE `previosDbName` CHARACTER ... ;

I have to change previosDbName to a value, passed as a parameter. I try to use sed, but it does not work. Here is a script:

echo $3

export updateValue="ALTER DATABASE \`$3\`"

echo $updateValue

sed -i 's/ALTER DATABASE `\(.*\)`/${updateValue}/' $4

Here how I run it:

./test.sh dbP dbN medi_6_0 full_path_to_test.sql

The output of the script tells us, that updateValue is a correct one:

medi_6_0
ALTER DATABASE `medi_6_0`

But the result string:

${updateValue} CHARACTER ...;

Instead of:

ALTER DATABASE `medi_6_0` CHARACTER ...;

I've also tried to use double quotes, but it did not help:

sed -i 's/ALTER DATABASE `\(.*\)`/"${updateValue}"/' $4

Update: Solution offered by @Sundeep helped:

sed -i 's/ALTER DATABASE `\(.*\)`/'"${updateValue}"'/' $4

None of solutions described in: How to use variables in a command in sed? Helped. As most of solutions (did not check all of them), offered in this topic.

Community
  • 1
  • 1
Alexandr
  • 9,213
  • 12
  • 62
  • 102
  • 1
    double quotes within single quotes are just another character, it won't do variable substitution... try ``'s/ALTER DATABASE `\(.*\)`/'"${updateValue}"'/'`` – Sundeep May 04 '17 at 13:21
  • Give a try to `sed -i -E "s/ALTER DATABASE (.*)/$updateValue/" $4` – George Vasiliou May 04 '17 at 13:21
  • @Sundeep, only your solution helped. Thank you very much! – Alexandr May 04 '17 at 13:39
  • Possible duplicate of [How to use variables in a command in sed?](http://stackoverflow.com/questions/19151954/how-to-use-variables-in-a-command-in-sed) – Benjamin W. May 04 '17 at 13:44
  • @BenjaminW., none of the solutions in that issue helped, I'll add it into the my original question – Alexandr May 04 '17 at 15:30
  • [This answer](http://stackoverflow.com/a/19152302/3266847) does the exact same quoting as Sundeep's suggestion, not sure how it is different in your opinion. – Benjamin W. May 04 '17 at 15:35

3 Answers3

0

You can use:

sed -iE "s/(ALTER DATABASE \`).*(\`)/\1$3\2/" file.sql
  • Use double quotes so that "$3" can be expanded by shell
  • Escape the reverse quotes, otherwise it tells shells to execute the text between ` and ` as a command.
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

To allow variable interpolation enclose sed expression with double quotes.
Also, backtick ` sign has a special meaning. Everything between backticks is evaluated. So it should be escaped with a backslash \

...
export updateValue="$3"
sed "s/\(ALTER DATABASE\) \`.*\`/\1 \`$updateValue\`/" $4
...

The output:

ALTER DATABASE `medi_6_0` CHARACTER ... ;
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

bash variables can only expand inside double quotes and backtick needs to be escaped, i.e.:

sed -i "s/\(ALTER DATABASE\) \`.*\`/\1 \`$3\`/" $4
# ALTER DATABASE `medi_6_0` CHARACTER ... ;

Options:

-i     Edit files in-place (otherwise sends to stdout)
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268