3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
sed帶雙引號(hào)的報(bào)價(jià)代碼:
$ sed "s/ones/one's/"<<<"ones thing"
one's thing
我不喜歡用數(shù)百個(gè)反斜杠來(lái)轉(zhuǎn)義代碼,這很傷我的眼睛。通常我是這樣做的:
$ sed 's/ones/one\x27s/'<<<"ones thing"
one's thing

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
一種技巧是使用相鄰字符串的外殼程序字符串連接,并使用外殼程序轉(zhuǎn)義對(duì)嵌入式引號(hào)進(jìn)行轉(zhuǎn)義:
sed 's/ones/two'\''s/' <<< 'ones thing'
two's thing
sed表達(dá)式中有3個(gè)字符串,然后外殼將它們縫合在一起:
sed 's/ones/two'
\'
's/'
希望對(duì)別人有幫助!

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
只需在sed命令的外部使用雙引號(hào)即可。
$ 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
如果字符串中有單引號(hào)和雙引號(hào),也可以。只是轉(zhuǎn)義雙引號(hào)。
例如,此文件包含帶單引號(hào)和雙引號(hào)的字符串。我將使用sed添加單引號(hào)并刪除一些雙引號(hào)。
$ 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 關(guān)注
- 2541 瀏覽
添加回答
舉報(bào)