打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
sed指定某行插入、追加、全局替换
有时候会有这样的需求,在指定的行后面或者是前面追加一行,这个时候可以使用sed来完成,具体用法如下
a\ 在指定的行后面追加一行
b\ 在指定的行前面追加一行

### 使用指定的行号追加内容,在使用行号的过程中,需要注意的问题有以下
### N;后面只能使用偶数,且不可以为0
###  a表示在指定的行后面追加一行
###  i表示在当前行插入一行,如果指定行为4,其实最终的结果插入行的位置是第三行。
sed -i 'N;2a\newline' 1.txt
sed -i 'N;2i\newline' 1.txt

[root@lanmp shell]# cat < eof=""> 1.txt> a> b> c> d> eof[root@lanmp shell]# sed -i 'N;2a2222' 1.txt[root@lanmp shell]# cat 1.txtab2222cd[root@lanmp shell]# sed -i 'N;2i2222' 1.txt[root@lanmp shell]# cat 1.txt2222ab2222cd[root@RS2 shell]# cat 1.txt11113333[root@RS2 shell]# sed -i '/^1111$/a\2222' 1.txt ; cat 1.txt111122223333[root@RS2 shell]# sed -i '/^1111$/i\0000' 1.txt ; cat 1.txt0000111122223333




下面是把所有匹配的字符都替换为指定的字符

[root@vmmonitor html]# cat -n 1.txt 1 11111111 2 222222222 3 22222222222 4 33333333 5 4 6 555555 7 888[root@vmmonitor html]# sed 3{s/2/3/} 1.txt1111111122222222232222222222333333334555555888[root@vmmonitor html]# vim 1.txt[root@vmmonitor html]# vim 1.txt[root@vmmonitor html]# cat 1.txt1111111122222222222233333332222222233222222222222223333334555555888[root@vmmonitor html]# sed 3{s/2/3/g} 1.txt1111111122222222233333333333333333333222222222222223333334555555888[root@SLAVE ~]# cat < eof=""> 1.txt> 1111> 222333333> 44444444445> eof[root@SLAVE ~]# sed -i 2{s/2/3/g} 1.txt[root@SLAVE ~]# cat 1.txt111133333333344444444445[root@SLAVE ~]# sed -i 3{s/4/5/g} 1.txt[root@SLAVE ~]# cat !$cat 1.txt111133333333355555555555


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux中通过Shell对文件中指定行进行增加、删除、追加、替换
SHELL 在指定行的前/后插入指定内容
不会这些题目,你好意思说会linux吗?
25个好用的Shell脚本常用命令分享
Shell中的grep、awk和sed的常用命令和语法
linux shell cut按列切分文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服