I have to following function
#------------------------------------------------------------------------------
# This function just executes one-liner sed command
# to add "line2" after the line that contains "line1"
# in file "file_name"
#------------------------------------------------------------------------------
def add_line2_after_line1_in_file(line1, line2, file_name, comment)
bash comment do
stripped_line2=line2.strip
user 'root'
code <<-EOC
sed -i '/#{line1}/a #{line2}' #{file_name}
EOC
only_if do File.exists? file_name and File.readlines(file_name).grep(/#{stripped_line2}/).size == 0 end
end
end
How can I call the function to produce this?
Before calling the function
This is a line
Indented line1 \
Indented line2 \
Line3
After calling the function
This is a line
Indented line1 \
Indented line2 \
Added line \
Line3
I tried this to no avail
add_line2_after_line1_in_file("line1", " line2 \\", "file", "comment")
Maybe a better function that does the same thing?