2016-12-01 38 views
0

我正在學習shell的sed。 我嘗試下面的代碼,如何通過sed刪除一行中的第N個目標單詞(='remove_mark')?

echo "one tworemove_markthree fourremove_markfive" | sed -E "s?(.*)remove_mark(.*)?\1\2?" 

我預期的這個輸出是

one twothree fourremove_markfive 

但上面的代碼的輸出以下,

one tworemove_markthree fourfive 

第一remove_mark被保持,但第二個被刪除。 但是我想刪除第一個。怎麼做?以及如何刪除所有匹配的目標詞?非常感謝你。

+1

似乎你的標題不會與您的問題匹配 - 你要刪除哪個字,即是你的期望的輸出?如有必要,請[編輯]您的問題以向我們確切顯示您想要的內容並更改標題。 –

回答

1

只需匹配remove_mark並且不需要替換。

$ echo "one tworemove_markthree fourremove_markfive" | sed 's/remove_mark//' 
one twothree fourremove_markfive 

要刪除所有目標,用g(全球)修改。

$ echo "one tworemove_markthree fourremove_markfive" | sed 's/remove_mark//g' 
one twothree fourfive 
+0

謝謝你告訴我一個簡單的解決方案。 – mora

+1

@mora歡迎:) – nu11p01n73R

相關問題