且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用sed替换文件中第n次出现的字符串

更新时间:2023-01-11 21:06:04

这可能对你有用(GNU sed):

This might work for you (GNU sed):

sed ':a;$!{N;ba};s/jack/'"$var"'/2' file

这会将文件拖入内存,然后将第二次出现的 jack 替换为 $var.

This slurps the file into memory and then substitutes the second occurence of jack for $var.