2012-11-19 87 views
2

我需要用像這樣的字符串替換像"'a"這樣的文件字符串。 實際上,我需要刪除雙引號。shell腳本中雙引號內的單引號

我正在考慮使用sed來做到這一點,但直到現在我找不到解決方案:我想我因爲引號而導致一些語法錯誤。

回答

2

如果你只是需要從文件中刪除所有的雙引號字符,那麼你可以使用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' 

更新:

如果要更換特定實例的"'a"'a然後你可以使用sed

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

但是,我懷疑你是在比一般的a字符更換引號標記更普遍。這sed命令將'anyhting取代"'anything"任何實例:

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

謝謝你,但這種方式我刪除文件的所有雙引號。我想刪除只有一個包裝'一個。我想保留其他 – user1835630

+0

是的,確切地說。我想要的輸出應該是:這是一個測試' 某些「其他」 不會觸及單個「引號」 – user1835630

+0

請參閱我的更新:-) –

0

這似乎爲我工作

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

這可能爲你工作(GNU SED):

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

你可以使用:

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

042是雙引號的八進制值。

如下測試:

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