Replace with sed on Mac OS X Leopard does not do what is expected

I am trying to replace \ "(double backslash quote) with '(quote) using sed.

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

The team does not work as expected. It replaces everything "in a text file, not just".

It does the same thing as sed "s / \" / \ '/ g "file.txt

I am working on Mac OS X Leopard.

Does anyone have a key?

+4
source share
3 answers

You are dealing with the notorious issue of shell quoting. Try using single quotes around s // g instead or add extra escape:

 sed "s/\\\\\"/\'/g" 
+7
source

Quote: problems with bash are fun.

  $ cat input
 "This is an \" input file \ "that has quoting issues."
 $ sed -e 's / \\ "/'" '"' / g 'input
 "This is an 'input file' that has quoting issues." 

Note that there are three lines connected together to make a sed script:

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

The first and last are quoted with single quotes, and the middle are quoted with double quotes.

Matthew's team works by combining two lines instead of three:

  • s/\\"/
  • '/g

where the first is a single-cascade, and the second is a double quote.

+2
source

no need to use too many quotes. \ 042 is the octal for " , and \ 047 is the octal for the single quote

 awk '{gsub("\042","\047") }{print}' file 
0
source

Source: https://habr.com/ru/post/1301202/


All Articles