Linux | sed

Example

替换文本

将 dog 替换为 cat

1
sed 's/dog/cat' data1.txt
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.

使用多个命令来替换

操作:加-e,然后一条命令之后紧跟分号,然后再接下一条命令.

例子:将 brown 替换为 green,将 dog 替换为 cat.

1
sed -e 's/brown/green/; s/dog/cat/' data1.txt
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.

从文件读取命令

操作:-f script文件

例子:将 script1.sed 文件中的命令应用到文件的每一行

1
sed -f script1.sed data1.txt