sed编辑器基础之替换命令

来源:互联网 发布:淘宝和京东 编辑:程序博客网 时间:2024/06/08 02:11

s命令


s命令说白了就是替换命令,在给定的字符串中用反斜杠后面的字符串代替反斜杠前面的字符串。直接将字符串用管道交给sed即可,直接看例子:

kevin@kevin-HP-Compaq-Pro-4300-AiO-20-PC-APJ-CTO:~$ echo "This is a test" | sed 's/test/big test/'This is a big test

假如我们要处理文本,那么应该这么搞:

kevin@kevin-HP-Compaq-Pro-4300-AiO-20-PC-APJ-CTO:~$ cat dataThe quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.kevin@kevin-HP-Compaq-Pro-4300-AiO-20-PC-APJ-CTO:~$ sed 's/dog/cat/' dataThe 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.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.The quick brown fox jumps over the lazy cat.kevin@kevin-HP-Compaq-Pro-4300-AiO-20-PC-APJ-CTO:~$ cat dataThe quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.

命令很简单,把文件名放在命令后面就行了,输出了替换后的字符串。注意,这个时候替换的只是输出的字符串,文件中的dog没有被替换成cat,见输出。

e命令


还是看示例吧,看了一眼就明白了。

kevin@kevin-HP-Compaq-Pro-4300-AiO-20-PC-APJ-CTO:~$ sed -e 's/brown/green/; s/dog/cat/' dataThe 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.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.The quick green fox jumps over the lazy cat.

很明白了吧,就是一次性替换多个,但要注意:*命令之间必须用“;”分隔

还有你也可以这么搞,不想一次性把要替换的word都写出来,可以用bash shell的提示符来写:

kevin@kevin-HP-Compaq-Pro-4300-AiO-20-PC-APJ-CTO:~$ sed -e '> s/brown/green/> s/fox/elephant/> s/dog/cat/' dataThe quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.

bash shell是这么执行的,就是一旦发现了封尾的单引号,就开始执行命令。

以上无论这么折腾,都是现场敲命令,那我们能不能将脚本写好,然后再处理呢,答案当然是可以。

首先编辑好脚本script

s/brown/green/s/fox/elephant/s/dog/cat/

运行

kevin@kevin-HP-Compaq-Pro-4300-AiO-20-PC-APJ-CTO:~/shell$ sed -f script1 data The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.The quick green elephant jumps over the lazy cat.

总而言之,言而总之,替换来替换去只是替换STDOUT上的字符,文本中的字符不动。

0 0
原创粉丝点击