博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sed编辑器基础之替换命令
阅读量:4097 次
发布时间:2019-05-25

本文共 2964 字,大约阅读时间需要 9 分钟。

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上的字符,文本中的字符不动。

转载地址:http://gwhii.baihongyu.com/

你可能感兴趣的文章
不会 Java 注解 ? 看这一篇文章!
查看>>
Java开发必备 Git 分支开发:规范指南及完全学会Git的24堂课笔记
查看>>
Redis深度历险:核心原理和应用实践带你由入门走向精通
查看>>
全网首发:阿里云面试真题公开,想面阿里,按照这样做准备
查看>>
一篇文章吃透Java多线程编程核心技术,敢于直面面试官多线程问题
查看>>
开发五年,如何在一年内从13k到大厂30k,全靠这份《java面试宝典》!
查看>>
深入P8级别JAVA底层知识:你知道阿里P8需要掌握哪些技术吗?
查看>>
Java基础知识点总结
查看>>
Java的新未来:逐渐“Kotlin化”
查看>>
Java反射是什么?看这篇绝对会了!
查看>>
京东T9用一份900页SpringBoot笔记让你像搭积木一样构建系统架构
查看>>
目前开发中爆火的Java、Go和Rust间的比较!
查看>>
Java语言新特性?封印类
查看>>
MySQL深度解析,基础+高级篇数据库教程-从入门到入坟
查看>>
全网最全SpringBoot干货知识总结(超详细,建议收藏)
查看>>
同事牛逼啊,写了个隐藏 bug,我排查了 3 天才解决问题
查看>>
凭借这份文档,阿里Java岗四面直接定级P8,面经面试题分享
查看>>
Spring Boot在微服务中的最佳实践
查看>>
请把 .gitattributes 加到你的 Git 仓库中
查看>>
太赞了!美团T9终于整理出Java架构之完美设计实战开源文档
查看>>