Replace whole line containing a string using Sed
The Answer above:
sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo
Works fine if the replacement string/line is not a variable.
The issue is that on Redhat 5 the \
after the c
escapes the $
. A double \\
did not work either (at least on Redhat 5).
Through hit and trial, I discovered that the \
after the c
is redundant if your replacement string/line is only a single line. So I did not use \
after the c
, used a variable as a single replacement line and it was joy.
The code would look something like:
sed -i "/TEXT_TO_BE_REPLACED/c $REPLACEMENT_TEXT_STRING" /tmp/foo
Note the use of double quotes instead of single quotes.