Loading

sed合并多条指令修改文本

需求

使用shell修改配置文件,需要删除最后一行并在某特定位置加上一行内容,例如:
修改前

This_is_the_beginning
...
End_of_file
Delete_this_line

修改后

This_is_the_beginning
New_content_here
...
End_of_file

思考

很容易想到sed -i进行文件操作,连续使用两次sed命令太长,希望合并。

实现

使用;对多条sed指令进行连接

sed -i '$ d;/This_is_the_beginning/a New_content_here' ~/config.yaml 

解析
$ d 删除最后一行
/foo/a bar 将bar插入到第一次匹配到的foo行后

参考

Combining two sed commands - Stack Overflow

posted @ 2021-07-28 11:41  azureology  阅读(259)  评论(0编辑  收藏  举报