linux命令-sed命令使用(4)

sed 编辑器的 更强大的功能。,插入,追加 修改,转化
sed 的基本使用
sed命令行格式为:
sed [-nefri] ' command' 输入文本

sed 中 插入,添加,修改, 转化
i a c y
i 会插入 在数据流 的前面,
a 会 追加 在数据流的后面。
c 修改
y 转化

[root@myCentOS ~]# echo "aaaabbbcccc"|sed 'i \Test Line one.'
Test Line one.
aaaabbbcccc
[root@myCentOS ~]# echo " aaaabbbcccc"|sed 'a\Test Line one.'
aaaabbbcccc
Test Line one.

抛砖引玉一下,下面来看看 如何用sed 插入的。

sed '[address]command\
new line ' filename
可以 用数字来指定行。
[root@myCentOS shell]# cat data7
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

This is an inserted line 插入到第四行前面
[root@myCentOS shell]# sed '4i\
This is an inserted line.' data7
This is line number 1.
This is line number 2.
This is line number 3.
This is an inserted line.
This is line number 4.
[root@myCentOS shell]# sed '4i\This is an inserted line.' data7
This is line number 1.
This is line number 2.
This is line number 3.
This is an inserted line.
This is line number 4.
[root@myCentOS shell]#

插入到第二行的后面。
[root@myCentOS shell]# sed '2 a\This is an inserted line.' data7
This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
This is line number 4.
[root@myCentOS shell]#

插入到尾行,只要用$ 代表 尾行就可以了。
[root@myCentOS shell]# sed '$a\
> this is the end. ' data7
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
this is the end.


那么 如何添加 多行文本呢 ?
[root@myCentOS shell]# sed '1a\
> aaaaaaaaaaaaaaa \
> bbbbbbbbbbbbbb \
> ccccccccccccc' data7
This is line number 1.
aaaaaaaaaaaaaaa
bbbbbbbbbbbbbb
ccccccccccccc
This is line number 2.
This is line number 3.
This is line number 4.


当然 sed 也可以进行 修改,修改 整行的文本内容。
命令其实 和插入,追加类似 此时用 c ---> change
[root@myCentOS shell]# sed '2c\
This is a changed line of text.' data7
This is line number 1.
This is a changed line of text.
This is line number 3.
This is line number 4.

当然 也可以用文本模式 来进行寻址。
[root@myCentOS shell]# sed ' /number 3/c\
> This is a changed line of text. ' data7
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.


当然你也可以使用 地址空间 进行寻址
[root@myCentOS shell]# sed '2,3c \
> This is a changed line of text. ' data7
This is line number 1.
This is a changed line of text.
This is line number 4.
此时 会把 2到3行所有的内容 ,修改为一行 This is a changed line of text.



sed 中的转换命令
[address]y/inchars/outchars/
转换命令会进行inchars 和outchars值的 一对一映射。 inchars 的第一个字符 会被转化为 outchars 中的第一个字符,第二个字符会转化成 outchars的第二个字符。 这个映射会一直持续到处理玩制定字符。 如果inchars和outchars 的长度 不同,则sed编辑器会产生 一条错误消息。这样是不能够 转化的。
[root@myCentOS shell]# cat data8
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.
This is line number 6.
This is line number 7.
This is line number 8.
This is line number 9.
[root@myCentOS shell]# sed 'y/ 1234 567/ ABCd xyz/' data8
This is line number A.
This is line number B.
This is line number C.
This is line number d.
This is line number x.
This is line number y.
This is line number z.
This is line number 8.
This is line number 9.

1 转换成A, 2 转换为B, 3转换为C,4转换为d,... 7 转换为z 。

当然这里 也是可以指定一个范围的。比如 指定1到3行,进行转化。
[root@myCentOS shell]# sed ' 1,3y/1234567/ABCdxyz/' data8
This is line number A.
This is line number B.
This is line number C.
This is line number 4.
This is line number 5.
This is line number 6.
This is line number 7.
This is line number 8.
This is line number 9.


总结: 本文 sed 最基本的用法,插入,追加,修改 等基本的用法。 其中a i c 都是用 \ 斜线, y 这个 是用 / y/inchars/outchars/ ,并且 注意inchars 和outchars 是 一一对应的关系


培训PPT:培训Sed基础入门.pptx
链接:http://pan.baidu.com/s/1skKXNy9 密码: i7fi


分享快乐,留住感动。 2017年 10月 10日 星期二 19:43:15 CST --biaoge


全部评论

相关推荐

09-22 19:21
南京大学 Java
牛客96763241...:刚刚想说才投十几个,养生呢,结果一看是南大本硕✌️,肯定没有问题的
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
11-06 12:53
吉林大学 Java
如题,ip属地末九,计算机科班大三本科生。想找一段寒假实习,也是第一次找实习。 从大二暑假7月开始准备Java后端,前期有点磨叽,导致现在手忙脚乱。目前第二个项目黑马点评快写完了,第一个项目是苍穹外卖(两个项目都是烂大街的,这就很头大)。算法题在lc上从大二至今陆续刷了将近六百题,hot100已过一遍,面试150目前刷了一半。八股刚看了不到一周,想请教一下各位牛友,这一版简历哪些地方需要继续改进,接着优化? 同时,是现在立即开始投递,边投边背八股,完善项目。还是说八股再背个小半个月再开始投递比较好一点,我现在担心的是到了这个月下旬或者12月再开始投递简历面试会有点晚,听同学说到年底hc数量会大...
mikeu04:简历顶部留名字即可,你写“后端开发实习生-Java”就是把自己的方向限制死了。我建议把这揉在个人简介里,说你对后端开发充满热情就行。性别出生年份以及微信号不是必须的。 把个人简介从教育背景里拿出来,第一个写。你的个人简介有点太泛了。把“爱好中长跑”去了,加点数字(“拥有xxx年的xxx经历”),加点你最熟的几个语言或技术栈。和别人的简介区分开来。 专业技能放项目经历前面。面试官一般会优先看这个再往下看你做了什么项目来考察你是否具备这些技能。实习我不是很清楚,但像Redis, JVM, 消息模型,计算机网络这些属于基本知识。你如果了解GCP, AWS, Docker 这些实际生产工具就可以把八股知识换掉。 项目简介可以和工作内容揉在一起。项目简介还是太长了,就一句话,“开发了一个基于【1,2个主要框架】为【目标客户群体】的【产品类型】, 实现了【产品价值】”。产品价值不是功能。比如一个在线计算器,它的功能是算数,但它的价值可以是让人在没带计算器的情况下算数(可访问性)或比手算效率提升了80%。工作内容多加点数字,你这个产品有多少人用了?浏览量是多少?技术上xxx性能提升了多少%?(实在想不出来就丢给deepseek :) 11 月理论上秋招已经结束了。八股是背不完的。无脑投,刷笔试,中了面试邀请就突击面经八股,没问题的。
大厂面试问八股多还是项目...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务