Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed replace single/double quoted text?

Tags:

text

replace

sed

Having some difficulty in replacing some single/double quoted text with sed and was wondering what's the correct method for these 2 examples

to change Memached.ini file contents from

[server]
server[] = "localhost:11211"

to

[server]
server[] = "localhost:11211"
server[] = "localhost:11212"

and to change memcache.php file contents for these lines from

define('ADMIN_USERNAME','username');    // Admin Username
define('ADMIN_PASSWORD','password');    // Admin Password

$MEMCACHE_SERVERS[] = 'mymemcache-server1:11211'; // add more as an array
$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

to

define('ADMIN_USERNAME','myusername');  // Admin Username
define('ADMIN_PASSWORD','mypassword');      // Admin Password

$MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
$MEMCACHE_SERVERS[] = 'localhost:11212'; // add more as an array

I tried for example

sed -i 's/'ADMIN_USERNAME','memcache'/'ADMIN_USERNAME','u'/g' /var/www/html/memcache.php

while command runs, memcache.php file isn't changed at all ?

like image 524
p4guru Avatar asked Feb 02 '11 02:02

p4guru


People also ask

How do you use sed to replace single quotes?

Just use " for the outer quotes in your sed and you can then use ' in the replacement. You don't want "+ unless you might have more than one consecutive " and you want to replace all of them. If you do, use sed -r "s/\\\"+/\\'/g" .

How do you remove double quotes in sed?

A single line sed command can remove quotes from start and end of the string. The above sed command execute two expressions against the variable value. The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string.

Can sed use double quotes?

@DummyHead Here's another approach: if you use single quotes, the sed command is exactly as you type it. If you use double quotes, you must take into account all shell substitutions and elaborations to "visualize" what command is eventually passed on to sed.

How do you change a single quote to a double quote in Unix?

use double quotes as ilkkachu suggests: "s/'/\"/g"


2 Answers

You can replace the single quotes in the sed command with double-quoted single quotes. The shell sees a single quote as ending a string. So, let it. You had

sed -i 's/'ADMIN_USERNAME','memcache'/'ADMIN_USERNAME','u'/g' /var/www/html/memcache.php

But, if you replace the ' in the sed command with '"'"', then shell will see the first ' as ending the first single-quoted string, then "'" as a double-quoted single quote, and then the last ' as a beginning of a new single-quoted string. That'd be

sed -i 's/'"'"'ADMIN_USERNAME'"'"','"'"'memcache'"'"'/'"'"'ADMIN_USERNAME'"'"','"'"'u'"'"'/g' /var/www/html/memcache.php

You should also be able to do '\'' in place of the ' within the command, for the same reason.

sed -i 's/'\''ADMIN_USERNAME\'',\''memcache\''/\''ADMIN_USERNAME\'',\''u\''/g' /var/www/html/memcache.php

But really, it'd be better to use an alternative mechanism. I'd suggest defining the source and target strings as variables, and then put those in the sed string.

SRC="'ADMIN_USERNAME','memcache'"
DST="'ADMIN_USERNAME','u'"
sed -i "s/$SRC/$DST/g" /var/www/html/memcache.php

That's way more readable, and it makes it easier for you to handle the quoting mess in a sane way with bite-sized chunks. Yay "shell variable contents aren't subject to word expansion unless you force it" knowledge. :)

Make sure you don't put a / in the $SRC or $DST variables, though. ;)

like image 144
dannysauer Avatar answered Sep 22 '22 22:09

dannysauer


You'll have to use hex escapes, for example, to do those replacements.

$ echo "'foo'" | sed 's/\x27foo\x27/\x27bar\x27/'
'bar'

You could also use octal escapes: \o047 (that's a lower-case "Oh") or decimal escapes: \d39.

like image 40
Dennis Williamson Avatar answered Sep 24 '22 22:09

Dennis Williamson