sed编辑器基础之替换命令

2 篇文章 0 订阅

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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值