How to Replace Single Quotes with Another Character in Sed

How do I replace single quotes with another character in sed?

Try to keep sed commands simple as much as possible.
Otherwise you'll get confused of what you'd written reading it later.

#!/bin/bash
sed "s/'/ /g" myfile.txt

Sed to replace single quotation mark

You cannot embed a single quote in a single-quoted string, no matter now many escapes you use: 3.1.2.2 Single Quotes

You can use sed's -e option to chain the commands and give you the most quoting flexibility:

sed -e 's/"//g' -e 's/^ //' -e 's/ $//' -e "s/'/\\\'/"
# or
sed -e 's/"//g; s/^ //; s/ $//' -e "s/'/\\\'/"

How to use sed command to replace text between two single quotation marks in a file?

In the above example you could do this:

sed "/^Home/s/'[^']*'/'147 East Avenue'/" Address.txt

The /Home/ matches on the line of interest, and s// command replaces everything between the two single quotes including the quotes themselves. If you want to edit the file change sed to sed -i.

Replace all double quotes with single quotes

You need to pass the g flag to sed:

sed "s/\"/'/g"

sed replace single/double quoted text?

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. ;)

How do I escape slashes and double and single quotes in sed?

The s/// command in sed allows you to use other characters instead of / as the delimiter, as in

sed 's#"http://www\.fubar\.com"#URL_FUBAR#g'

or

sed 's,"http://www\.fubar\.com",URL_FUBAR,g'

The double quotes are not a problem. For matching single quotes, switch the two types of quotes around. Note that a single quoted string may not contain single quotes (not even escaped ones).

The dots need to be escaped if sed is to interpret them as literal dots and not as the regular expression pattern . which matches any one character.

sed replace with backslash, double quote, single quote

You can't escape a single quote inside single quotes. Your second attempt needs more backslashes: Remember, inside double quotes, the shell processes one layer of backslashes, so you have to double each backslash which should make it through to sed.

sed "s/\\\\\"/\\\\\\\\'/g" input.txt

After the shell has processed the double-quoted string, the script which ends up being executed is

s/\\"/\\\\'/g

where the first pair of backslashes produce a literal backslash in the matching regex, and each pair of backslashes in the replacement produce one literal backslash in the output.

Demo: https://ideone.com/XqfwbV



Related Topics



Leave a reply



Submit