I'm looking to add some lines before the end of a range in a file.
sed '/BEGIN/,/END/ /$/i \ SOME TEXT TO ADD' /path/to/foo
random text
BEGIN
foo
bar
END
random text
random text
BEGIN
foo
bar
SOME TEXT TO ADD
END
random text
Any ideas on how to accomplish this?
This might work for you (GNU sed):
sed '/BEGIN/,/END/!b;/END/iSOME TEXT TO ADD' file
If there are lines not between BEGIN and END print as normal otherwise if the line contains END insert some text.
Why don't you just look for the end tag, and insert before:
sed '/END/ iSOME TEXT TO ADD' /path/to/foo
Otherwise you must use several lines:
sed '/BEGIN/,/END/ {
/END/ iSOME TEXT TO ADD
}' /path/to/foo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With