在bash的文件中的两行之间添加一行(Add a line in between two lines in a file from bash)

编程入门 行业动态 更新时间:2024-10-26 14:30:44
在bash的文件中的两行之间添加一行(Add a line in between two lines in a file from bash)

我有一个名为user.rb文件,如:

class User < ApplicationRecord end

我想添加一个新行,添加行后会看起来:

class User < ApplicationRecord include Company end

但是我想从bash脚本或终端添加这个额外的行。 我尝试使用这个bash代码:

sed -i '' 's/end/include Company\n\end/' app/models/user.rb

但添加线后它不会返回新行。 请帮帮我。

I have file named user.rb as like:

class User < ApplicationRecord end

I want to add a new line and after adding line it will look :

class User < ApplicationRecord include Company end

But I want to add this additional line from bash script or terminal. I tried it with this bash code:

sed -i '' 's/end/include Company\n\end/' app/models/user.rb

But it does not return new line after adding line. Please help me.

最满意答案

在OSX上,您可以使用此sed命令:

sed -i.bak '/ApplicationRecord$/,/^end$/{/end$/i\ include Company }' file

On OSX you can use this sed command:

sed -i.bak '/ApplicationRecord$/,/^end$/{/end$/i\ include Company }' file

更多推荐

本文发布于:2023-08-07 04:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两行   文件   bash   Add   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!