在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 endI want to add a new line and after adding line it will look :
class User < ApplicationRecord include Company endBut 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.rbBut it does not return new line after adding line. Please help me.
最满意答案
在OSX上,您可以使用此sed命令:
sed -i.bak '/ApplicationRecord$/,/^end$/{/end$/i\ include Company }' fileOn OSX you can use this sed command:
sed -i.bak '/ApplicationRecord$/,/^end$/{/end$/i\ include Company }' file更多推荐
发布评论