$ cat file
cat cat
dog cat
dog puppy
dog cat
使用sed:
$ sed 's/dog/big_dog/' my_file > new_file
$ cat new_file
cat cat
big_dog cat
big_dog puppy
big_dog cat
我的目标是仅将第二个dog
替换为big_dog
,但使用以下代码未能实现:
$ sed 's/dog/big_dog/2' my_file > new_file
cat
dog cat
dog puppy
dog cat
我如何只替换第二次出现的内容,即:
cat
dog cat
big_dog puppy
dog cat
sed -i '0,/dog/! {0,/dog/ s/dog/big_dog/}' my_file
- Hashbrown