使用数字标记用来指定old中特定的部分。只有old中第n处出现的关键词分才会被替换。每行都从头开始算,n可以是1512中的任意数字。


比如,/11会替换某行出现在第11处的关键词。

下面是一些例子:

把第二次出现的小写字母a替换成大写字母A

$ sed 's/a/A/2'employee.txt

101,John Doe,CEO

102,Jason Smith,IT MAnager

103,RajReddy,SysAdmin

104,AnandRAm,Developer

105,JaneMiller,SAles Manager


在这个例子里,创建包含如下三行的文件:

$ visubstitute-locate.txt

locate command isused to locate files

locate command usesdatabase to locate files

locate command canalso use regex for searching


在你刚创建的文件中,把第二次出现的locate替换成find

$ sed's/locate/find/2' substitute-locate.txt

locate command isused to findfiles

locate command usesdatabase to findfiles

locate command canalso use regex for searching


注意:上例中的第三行,只有一处有locate,所以第三行没有任何的更改。