107

From what I can find, when you use single quotes everything inside is considered literal. I want that for my substitution. But I also want to find a string that has single or double quotes.

For example,

sed -i 's/"http://www.fubar.com"/URL_FUBAR/g'

I want to replace "http://www.fubar.com" with URL_FUBAR. How is sed supposed to recognize my // or my double quotes?

Thanks for any help!

EDIT: Could I use s/\"http\:\/\/www\.fubar\.\com\"/URL_FUBAR/g ?

Does \ actually escape chars inside the single quotes?

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
KRB
  • 4,875
  • 17
  • 42
  • 54

11 Answers11

130

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.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • great answer dood, I just have a quick question. How would I do single quotes in place of my doubles. I have instances in my code where it is either single or double I can just run this command twice but what's the syntax for escaping a single quote? – KRB Sep 22 '11 at 16:08
  • Escaping single quotes did not work for me (bash on mac), but using different quote types for wrapping the command than the quote being replaced did work. eg: `sed 's/"/bla/g'` to replace `"` with `bla` or `sed "s/'/bla/g" to replace `'` with `bla` – rrr May 01 '18 at 19:47
  • 5
    @rrr A single quoted string can not contain an embedded single quote. Changing the outer quotes (and escaping any special characters that the shell now might be interested in interpreting) is one way to solve that. Another is to break out of the single quoted string and concatenate the string with `"'"` (a double quoted single quote) before continuing with the rest of the single quoted string, as in `'you'"'"'d better do that'`. – Kusalananda Mar 21 '19 at 20:19
41

Regarding the single quote, see the code below used to replace the string let's with let us:

command:

echo "hello, let's go"|sed 's/let'"'"'s/let us/g'

result:

hello, let us go

TRiG
  • 10,148
  • 7
  • 57
  • 107
tony duan
  • 477
  • 4
  • 5
  • 3
    There's a ***much*** easier way to do this. Try: `echo "hello, let's go" | sed s/let\'s/let us/g`. You don't actually *have to* have the parameter for `sed` in single-quotes. – Tripp Kinetics May 26 '21 at 22:30
21

My problem was that I needed to have the "" outside the expression since I have a dynamic variable inside the sed expression itself. So than the actual solution is that one from lenn jackman that you replace the " inside the sed regex with [\"].

So my complete bash is:

RELEASE_VERSION="0.6.6"

sed -i -e "s#value=[\"]trunk[\"]#value=\"tags/$RELEASE_VERSION\"#g" myfile.xml

Here is:

# is the sed separator

[\"] = " in regex

value = \"tags/$RELEASE_VERSION\" = my replacement string, important it has just the \" for the quotes

goncalotomas
  • 1,000
  • 1
  • 12
  • 29
Denis
  • 1,179
  • 10
  • 13
13

It's hard to escape a single quote within single quotes. Try this:

sed "s@['\"]http://www.\([^.]\+).com['\"]@URL_\U\1@g" 

Example:

$ sed "s@['\"]http://www.\([^.]\+\).com['\"]@URL_\U\1@g" <<END
this is "http://www.fubar.com" and 'http://www.example.com' here
END

produces

this is URL_FUBAR and URL_EXAMPLE here
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

Escaping a double quote can absolutely be necessary in sed: for instance, if you are using double quotes in the entire sed expression (as you need to do when you want to use a shell variable).

Here's an example that touches on escaping in sed but also captures some other quoting issues in bash:

# cat inventory
PURCHASED="2014-09-01"
SITE="Atlanta"
LOCATION="Room 154"

Let's say you wanted to change the room using a sed script that you can use over and over, so you variablize the input as follows:

# i="Room 101" (these quotes are there so the variable can contains spaces)

This script will add the whole line if it isn't there, or it will simply replace (using sed) the line that is there with the text plus the value of $i.

if grep -q LOCATION inventory; then 
## The sed expression is double quoted to allow for variable expansion; 
## the literal quotes are both escaped with \ 
    sed -i "/^LOCATION/c\LOCATION=\"$i\"" inventory
## Note the three layers of quotes to get echo to expand the variable
## AND insert the literal quotes
else 
    echo LOCATION='"'$i'"' >> inventory
fi

P.S. I wrote out the script above on multiple lines to make the comments parsable but I use it as a one-liner on the command line that looks like this:

i="your location"; if grep -q LOCATION inventory; then sed -i "/^LOCATION/c\LOCATION=\"$i\"" inventory; else echo LOCATION='"'$i'"' >> inventory; fi
1

Aside: sed expressions containing BASH variables need to be double (")-quoted for the variable to be interpreted correctly.

If you also double-quote your $BASH variable (recommended practice)

... then you can escape the variable double quotes as shown:

sed -i "s/foo/bar ""$VARIABLE""/g" <file>

I.e., replace the $VARIABLE-associated " with "".

(Simply -escaping "$VAR" as \"$VAR\" results in a "-quoted output string.)


Examples

$ VAR='apples and bananas'
$ echo $VAR
apples and bananas

$ echo "$VAR"
apples and bananas

$ printf 'I like %s!\n' $VAR
I like apples!
I like and!
I like bananas!

$ printf 'I like %s!\n' "$VAR"
I like apples and bananas!

Here, $VAR is "-quoted before piping to sed (sed is either '- or "-quoted):

$ printf 'I like %s!\n' "$VAR" | sed 's/$VAR/cherries/g'
I like apples and bananas!

$ printf 'I like %s!\n' "$VAR" | sed 's/"$VAR"/cherries/g'
I like apples and bananas!

$ printf 'I like %s!\n' "$VAR" | sed 's/$VAR/cherries/g'
I like apples and bananas!

$ printf 'I like %s!\n' "$VAR" | sed 's/""$VAR""/cherries/g'
I like apples and bananas!

$ printf 'I like %s!\n' "$VAR" | sed "s/$VAR/cherries/g"
I like cherries!

$ printf 'I like %s!\n' "$VAR" | sed "s/""$VAR""/cherries/g"
I like cherries!

Compare that to:

$ printf 'I like %s!\n' $VAR | sed "s/$VAR/cherries/g"
I like apples!
I like and!
I like bananas!


$ printf 'I like %s!\n' $VAR | sed "s/""$VAR""/cherries/g"
I like apples!
I like and!
I like bananas!

... and so on ...

Conclusion

My recommendation, as standard practice, is to

  • "-quote BASH variables ("$VAR")
  • "-quote, again, those variables (""$VAR"") if they are used in a sed expression (which itself must be "-quoted, not '-quoted)
$ VAR='apples and bananas'

$ echo "$VAR"
apples and bananas

$ printf 'I like %s!\n' "$VAR" | sed "s/""$VAR""/cherries/g"
I like cherries!

Victoria Stuart
  • 4,610
  • 2
  • 44
  • 37
1

What finally worked for me was to use sed's HEX option to represent:

Single quote:

echo "book'" |sed -n '/book[\x27]/p'

book'

Double quotes solution

cat testfile

"http://www.fubar.com"

sed -i 's|"http://www\.fubar\.com"|URL_FUBAR|g' testfile

cat testfile

URL_FUBAR

Odrai
  • 2,163
  • 2
  • 31
  • 62
Joshro
  • 39
  • 5
0
Prompt% cat t1
This is "Unix"
This is "Unix sed"
Prompt% sed -i 's/\"Unix\"/\"Linux\"/g' t1
Prompt% sed -i 's/\"Unix sed\"/\"Linux SED\"/g' t1
Prompt% cat t1
This is "Linux"
This is "Linux SED"
Prompt%
SachinH
  • 26
  • 2
  • Just use \(back slash) before "(double quotes).. and use -i option – SachinH Apr 27 '17 at 12:40
  • 1
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 27 '17 at 14:46
0

You can use %

sed -i "s%http://www.fubar.com%URL_FUBAR%g"
Andrey
  • 1,528
  • 14
  • 12
-5

You need to use \" for escaping " character (\ escape the following character

sed -i 's/\"http://www.fubar.com\"/URL_FUBAR/g'
Guilhem Hoffmann
  • 962
  • 5
  • 13
-9

May be the "\" char, try this one:

sed 's/\"http:\/\/www.fubar.com\"/URL_FUBAR/g'
Pang
  • 9,564
  • 146
  • 81
  • 122
  • 4
    proposing a wild guess is not a solution. you can add such guesses in comments instead - or even better - try it out first – SeriousM Jan 13 '17 at 23:49