Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a line after another line in a file, in Ruby?

Updated description to be clearer.

Say I have a file and it has these lines in it.

one
two
three
five

How do I add a line that says "four" after the line that says "three" so my file now looks like this?

one 
two
three
four
five
like image 789
Chris F Avatar asked Oct 27 '25 20:10

Chris F


1 Answers

Assuming you want to do this with the FileEdit class.

Chef::Util::FileEdit.new('/path/to/file').insert_line_after_match(/three/, 'four')
like image 86
Tejay Cardon Avatar answered Oct 30 '25 12:10

Tejay Cardon