0

I am trying to use sed to replace the expression 'root'@'localhost' with 'root'@'%' with no success. Could someone please help me with the command?

I tried the following:

sed -i ’s#\’root\’@\’localhost\’#\’root\’@\’%\’#g’  xyz.sql
sed: -e expression #1, char 1: unknown command: `?'

sed -i -e ’s/localhost/%/g’ xyz_2616.sql 
sed: -e expression #1, char 1: unknown command: `?'
beginnercoder
  • 41
  • 1
  • 3

2 Answers2

0

First, make sure you're using a single quote. (a.k.a. ’ or unicode 8217) is not the same as ASCII character 39, '.

Next, you can't escape single quotes inside single quotes. Here's an answer I wrote about that some time ago.

You can, however, put single quotes inside double quotes, or escape them outside your single quoted string. For example, either of the following might work:

sed -e "s/'root'@'localhost'/'root'@'%'/g" file.sql

or

sed -e 's/'\''root'\''@'\''localhost'\''/'\''root'\''@'%'\''/g' file.sql

Alternately, you could substitute just the portion you're interested in, trusting that it doesn't appear elsewhere on the same line:

sed -e '/root.@.localhost/s/localhost/%/' file.sql
Community
  • 1
  • 1
ghoti
  • 45,319
  • 8
  • 65
  • 104
0

The character doesn't look like a single quote (') to me. Make sure that you are using single quotes.

The character you're actually typing is a "right single quote mark". What we refer to as a "single quote" is actually an "apostrophe".

The following should work:

sed -e 's/localhost/%/g' rice_2616.sql 

or, your first alternatives but with double quotes to avoid having to escape the embedded single quotes (which I presume are apostrophes):

sed -e "s/'root'@'localhost'/'root’@'%'/g" rice_2616.sql 
Kusalananda
  • 14,885
  • 3
  • 41
  • 52