LMLPHP后院

Linux Bash Sed 的使用

文本文件内容 a.txt。

this is a dog
this is a dog
this is a dog
this is a cat
this is a cat
this is a cat
this is a dog

sed 按照行替换,将第 2,3 行的 dog 替换为 cat:

$ sed '2,3s/dog/cat/' a.txt
this is a dog
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a dog

按照正则条件进行替换,将含有 dog 一行的 this 替换为 that:

$ sed '/dog/s/this/that/' a.txt
that is a dog
that is a dog
that is a dog
this is a cat
this is a cat
this is a cat
that is a dog
2024-12-22 00:55:06 1734800106 0.002699