开发者

replace with sed on Mac OS X Leopard doesn't do what expected

开发者 https://www.devze.com 2022-12-20 06:10 出处:网络
I\'m trying to replace \\\" (backslash doouble quote) by \' (quote) using sed. sed \"s/\\\\\\\"/\\\'/g\" file.txt

I'm trying to replace \" (backslash doouble quote) by ' (quote) using sed.

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

The command doesn't work as开发者_开发百科 expected. It replaces all the " in the text file, not only the \".

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

I'm working on Mac OS X Leopard.

Does any one have a clue?


You're dealing with the infamous shell quoting problem. Try using single quotes around the s//g instead, or add an extra escape:

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


Quoting issues 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 strings butted together to make the sed script:

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

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

Matthew's command works by joining two strings instead of three:

  • s/\\"/
  • '/g

where the first is single-quoted and the second double-quoted.


don't have to use too many quotes. \042 octal is for " and \047 is octal for single quote

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号