简体   繁体   English

shell脚本中双引号内的单引号

[英]single quote inside double quotes in shell script

I would need to replace in a file strings like "'a" with strings like 'a .我需要用像'a这样'a字符串替换像"'a"这样的文件字符串。 In practice, I need to remove the double quotes.在实践中,我需要删除双引号。

I was thinking to use sed to do it, but I could not find a solution til now: I guess I am making some syntax errors because of the quotes.我正在考虑使用 sed 来做到这一点,但直到现在我找不到解决方案:我想我因为引号而犯了一些语法错误。

If you just need to remove all double quote characters from the file then you can use tr with the -d option:如果您只需要从文件中删除所有双引号字符,那么您可以将tr-d选项一起使用:

$ cat test.txt
this is a test "'a"
something "else"
doesn't touch single 'quotes'

$ cat test.txt | tr -d '"'
this is a test 'a
something else
doesn't touch single 'quotes'

Update:更新:

If you want to replace the specific instance of "'a" with 'a then you can use sed :如果你想用'a替换"'a"的特定实例,那么你可以使用sed

sed "s|\"'a\"|'a|g" test.txt
this is a test 'a
something "else"
doesn't touch single 'quotes'

However, I suspect that you are after something more general than just replacing quote markes around an a character.但是,我怀疑您追求的不仅仅是替换a字符周围的引号更通用。 This sed command will replace any instance of "'anything" with 'anyhting :sed命令将用'anyhting替换任何"'anything"实例:

sed "s|\"'\([^\"]\+\)\"|'\\1|g" test.txt
this is a test 'a
something "else"
doesn't touch single 'quotes'

这似乎对我有用

echo '"a"' | sed "s/\"a\"/\'a/"

这可能对你有用(GNU sed):

sed 's/"\('\''[^"]*\)"/\1/g' file

you could use :你可以使用:

perl -pe 's/\042//g' your_file

042 is octal value of double quotes. 042 是双引号的八进制值。

tested below:测试如下:

> cat temp
"'a"
> cat temp | perl -pe 's/\042//g'
'a
> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM