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
- sed编辑器基础之替换命令
- sed编辑器基础之替换命令(二)
- sed 之替换命令
- Shell基础之-sed命令
- linux基础之sed命令
- linux基础命令之sed
- linux基础命令之sed
- Linux基础之sed流编辑器详解
- sed 替换命令
- 字符串替换命令 sed
- sed 命令批量替换
- 查找和替换之sed流编辑器详解
- sed编辑器--命令
- 流编辑器sed命令
- sed使用 之 替换
- linux之sed基础命令详解
- sed编辑器基础
- 非交互式替换命令sed
- baseAction
- Facebook 登录自定义按钮
- selenium Rc的使用
- 前端页面制作流程分享与总结
- JQuery拼接html页面时JSONObject作为onclick参数如何传递?
- sed编辑器基础之替换命令
- 小强升职记 思维导图
- nRF52832 Power II 需核对
- http缓存
- TCP与UDP的区别
- Jasmine
- Bootstrap 学习之 (十一) ------ 分页
- 用dom4j解析xml文件并执行增删改查操作
- mysql数据表建立从表,使用外键时.删除从表数据,设置外键属性方能删除