仅替换 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. >>
答案1
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
。