3 回答

TA貢獻1829條經驗 獲得超9個贊
sed帶雙引號的報價代碼:
$ sed "s/ones/one's/"<<<"ones thing"
one's thing
我不喜歡用數百個反斜杠來轉義代碼,這很傷我的眼睛。通常我是這樣做的:
$ sed 's/ones/one\x27s/'<<<"ones thing"
one's thing

TA貢獻1851條經驗 獲得超3個贊
一種技巧是使用相鄰字符串的外殼程序字符串連接,并使用外殼程序轉義對嵌入式引號進行轉義:
sed 's/ones/two'\''s/' <<< 'ones thing'
two's thing
sed表達式中有3個字符串,然后外殼將它們縫合在一起:
sed 's/ones/two'
\'
's/'
希望對別人有幫助!

TA貢獻1840條經驗 獲得超5個贊
只需在sed命令的外部使用雙引號即可。
$ sed "s/ones/one's/" <<< 'ones thing'
one's thing
它也適用于文件。
$ echo 'ones thing' > testfile
$ sed -i "s/ones/one's/" testfile
$ cat testfile
one's thing
如果字符串中有單引號和雙引號,也可以。只是轉義雙引號。
例如,此文件包含帶單引號和雙引號的字符串。我將使用sed添加單引號并刪除一些雙引號。
$ cat testfile
"it's more than ones thing"
$ sed -i "s/\"it's more than ones thing\"/it's more than one's thing/" testfile
$ cat testfile
it's more than one's thing
- 3 回答
- 0 關注
- 2542 瀏覽
添加回答
舉報