Ed

替換 ed 中第二次出現的單詞/片語

  • June 3, 2022

僅替換 ed 中第二次/第三次出現的單詞/片語的語法是什麼?

He wanted to buy groceries from the shop, but preferred not having to go into the shop. >>
He wanted to buy groceries from the shop, but preferred not having to go into it. >>

ed,中的替換命令在s///命令字元串的末尾採用零個或多個標誌。如果標誌包含正整數n,則替換將作用於第n次匹配。

假設您想the shop用 string 替換目前行上第二次出現的 substring it,因此您將使用

s/the shop/it/2

來自編輯會話的快照可能如下所示,其中使用者查看目前行,p然後決定對第二次出現的 進行更改,the shop在編輯後再次顯示該行(使用命令的標誌p s///

p
He wanted to buy groceries from the shop, but preferred not having to go into the shop.
s/the shop/it/2p
He wanted to buy groceries from the shop, but preferred not having to go into it.

s///編輯命令的這一特性由sed.

引用自:https://unix.stackexchange.com/questions/704819