1

How can I make sed use double quotes (") as a match?

I try to replace "source/design to "source/design_new only without changing the second coding.

Input:

"source/design",  
"include/source/design",

Expect Output:

"source/design_new",    #only this line renaming  
"include/source/design"

I tried with command:

sed "s/\"source\/design/\"source\/design_new/g"

but it is complaining with an unmatched " error. Is there any way to use double quotes in sed?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
penguin blue
  • 11
  • 1
  • 1
  • 2
  • possible duplicate of [sed, foward slash in quotes](http://stackoverflow.com/questions/11147323/sed-foward-slash-in-quotes) – NeronLeVelu Apr 17 '15 at 09:03

3 Answers3

4

Basically, sed takes whatever follows the s as the separator (except newline or backslash). So you can use some any other character - let's say ; - as a separator instead of /.

Example:

sed 's;"source/design;"source/design_new;g' input_file
Rax
  • 785
  • 4
  • 14
2

Just use single quotes around the command instead of double quotes. This way you do not have to worry about how to handle them:

sed 's~"source/design~"source/design_new~g' file
    ^                                     ^

With your given input this command returns:

"source/design_new",  
"include/source/design",

Also note you can avoid escaping every / by using another separator in sed (I used ~, although it could also be | or another one).

fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

Welcome to tcsh. You can't include double quotes in doublequotes. End the double quotes and backslash the double quote:

sed "s/"\""source\/design/"\""source\/design_new/g"

Or, more readable (using a different separator to avoid the need to backslash slashes, and using no quotes as they're not needed for letters, slashes and underscores:

sed s=\"source/design=\"source/design_new=g
choroba
  • 231,213
  • 25
  • 204
  • 289