高效而轻松的sed命令_sed替换命令效率模式(1)

a \: 即append追加字符串,可将其后的字符加在所选择内容的后面
c \: 取代/替换字符串,可将其后内容替换至所选内容
d  : 即delete删除,该命令会将当前选中的行删除
i \: 即insert插入字符串,可将其后内容插入至所选内容前
p  : print即打印,该命令会打印当前选择的行到屏幕上
s  : 替换,通常s命令的用法是这样的:1,2s/old/new/g,将old字符串替换成new字符串

命令示例

假设有一个本地文件test.txt,文件内容如下:

[root@linuxprobe ~]$ cat test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end

本节将使用该文件详细演示每一个命令的用法。

a命令

[root@linuxprobe ~]$ sed '1a \add one' test.txt
this is first line
add one
this is secondline
this is third line
this is fourth line
this is fifth line
happy everyday
end

本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用 表示最后一行,比如 2 , 表示最后一行,比如2, 表示最后一行,比如2,表示第二行到最后一行中间所有的行(包含第二行和最后一行)。
本例的作用是在第一行之后增加字符串”add one”,从输出可以看到具体效果。

[root@linuxprobe ~]$ sed '1,$a \add one' test.txt
this is first line
add one
this is second line
add one
this is third line
add one
this is fourth line
add one
this is fifth line
add one
happy everyday
add one
end
add one

本例表示在第一行和最后一行所有的行后面都加上”add one”字符串,从输出可以看到效果。

[root@linuxprobe ~]$ sed '/first/a \add one' test.txt
this is first line
add one
this is secondline
this is third line
this is fourth line
this is fifth line
happy everyday
end

本例表示在包含”first”字符串的行的后面加上字符串”add one”,从输出可以看到第一行包含first,所以第一行之后增加了”add one”

[root@linuxprobe ~]$ sed '/^ha.*day$/a \add one' test.txt
this is first line
this is secondline
this is third line
this is fourth line
this is fifth line
happy everyday
add one
end

本例使用正则表达式匹配行,^ha.*day$表示以ha开头,以day结尾的行,则可以匹配到文件的”happy everyday”这样,所以在该行后面增加了”add one”字符串。

i命令

i命令使用方法和a命令一样的,只不过是在匹配的行的前面插入字符串,所以直接将上面a命令的示例的a替换成i即可,在此就不啰嗦了。

c命令

[root@linuxprobe ~]$ sed '$c \add one' test.txt
this is first line
this is secondline
this is third line
this is fourth line
this is     fifth line
happy everyday
add one

本例表示将最后一行替换成字符串”add one”,从输出可以看到效果。

[root@linuxprobe ~]$ sed '4,$c \add one' test.txt
this is first line
this is secondline
this is third line
add one

本例将第四行到最后一行的内容替换成字符串”add one”。

[root@linuxprobe ~]$ sed '/^ha.*day$/c \replace line' test.txt
this is first line
this is secondline
this is third line
this is fourth line
this is fifth line
replace line
end

本例将以ha开头,以day结尾的行替换成”replace line”。

d命令

[root@linuxprobe ~]$ sed '/^ha.*day$/d' test.txt
this isfirst line
this issecond line
this isthird line
this isfourth line
this isfifth line
end

本例删除以ha开头,以day结尾的行。

[root@linuxprobe ~]$ sed '4,$d' test.txt
thisis first line
thisis second line
thisis third line

本例删除第四行到最后一行中的内容。

p命令

[root@linuxprobe ~]$ sed -n '4,$p' test.txt
thisis fourth line
thisis fifth line
happy everyday
**先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里**

**深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/f9ac0d4c189505735d134fbb18a4d683.png)
![img](https://img-blog.csdnimg.cn/img_convert/7b7baf35d9e6116a6f3bc719251119f3.png)
![img](https://img-blog.csdnimg.cn/img_convert/4ddbebcaf720953a252a18bd1c6a719e.png)
![img](https://img-blog.csdnimg.cn/img_convert/3bb590549747dbedb4bb5cf5be2f937f.png)
![img](https://img-blog.csdnimg.cn/img_convert/937d6c3c30e64277eadbac2d220c86e7.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

8482)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值