LMLPHP后院

Linux Bash Sed 的使用

原文链接 : https://blog.lmlphp.com/tutorial/1/SHELL/item/23/Use_of_Linux_Bash_Sed  来自 : LMLPHP后院

文本文件内容 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
2018-05-25 03:50:38 1527191438 0.003791