Anonymous Anonymous - 2 years ago 479
Bash Question

Replace whole line containing a string using Sed

I have a text file which has a particular line something like

sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext

I need to replace the whole line above with

This line is removed by the admin.

The search keyword is

I need to write a shell script for this. How can I achieve this using

Answer Source

You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed:

sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download