How can I avoid double and single quotes in sed?

From what I can find when you use single quotes, everything inside is considered a literal. I want this for my replacement. But I also want to find a string with 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 should sed recognize my // or my double quotes?

Thanks for any help!

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

Does \ escape characters inside single quotes?

+63
command-line bash sed
Sep 22 '11 at 15:46
source share
9 answers

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

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

or

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

Double quotes are not a problem. To match single quotes, toggle the two types of quotes around. Note that a string in single quotes may not contain single quotes (not even escaped).

Points must be escaped if sed should interpret them as points, and not as a regular expression pattern . that matches any one character.

+87
Sep 22 '11 at 16:03
source share

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

Team:

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

result:

hi let's go

+25
Jul 15 '14 at 6:31
source share

It is hard to avoid single quotes in single quotes. Try the following:

 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 
+12
Sep 22 '11 at 17:15
source share

My problem was that I had to have a "" outside the expression, since I have a dynamic variable inside the sed expression itself. So, the real solution is that from lenn jackman you replace " inside the serial expression [\"] .

So my full bash:

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

Here:

# - sed delimiter

[\"] = " in regex

value = \"tags/$RELEASE_VERSION\" = my replacement string, it is important that it has only \" for quotes

+9
May 17 '13 at 13:00
source share

In sed, you may need to get rid of the double quote: for example, if you use double quote in the entire sed expression (as you need to do when you want to use a shell variable).

Here is an example that affects escaping in sed, but also fixes some other quoting issues in bash:

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

Suppose you wanted to change a room using a sed script, which you can use again and again, so you change the input as follows:

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

This script will add the entire line if it is not there, or 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 

PS I wrote the above script on several lines to make the comments syntactic, but I use it as a single line on the command line, which 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
Jun 15 '16 at 20:54 on
source share
 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% 
0
Apr 27 '17 at 12:34 on
source share

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

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

  • Bash Variables, Commands Affected by Numeric File and Folder Names

... then you can escape double quotes as shown in the picture:

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

Those. replace the associated $ VARIABLE " with "" .

(Just -escaping "$VAR" as \"$VAR\" produces the output string " -quoted.)




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 passing 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 this 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, these variables ( ""$VAR"" ), if used in the 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! 
0
May 09 '19 at 20:07
source share

You need to use the character "to escape" (\ escape the next character

 sed -i 's/\"http://www.fubar.com\"/URL_FUBAR/g' 
-four
Sep 22 '11 at 15:55
source share

Could be a "\" char, try the following:

 sed 's/\"http:\/\/www.fubar.com\"/URL_FUBAR/g' 
-7
Sep 22 '11 at 15:51
source share



All Articles