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
2019-10-20 11:09:38 1571540978 0.007485