Delete to end of line after a match, keep lines not matched

Youri_Margarine picture Youri_Margarine · Jun 25, 2013 · Viewed 41.5k times · Source

I have a file of the following form:

interesting text-MIB blah blah blah
VERY INTERESTING TEXT
interesting text-MIB blah blah blah

In each line containing the "-MIB" string, I would like to delete the text following this string, until the end of the line.

The following command seems to work as intended:

sed -i -n -e 's/-MIB.*$/-MIB/p' ./myfile

My problem is that it also deletes the lines where my pattern is not found. Here, the line "VERY INTERESTING TEXT" would be deleted. I am not really familiar with sed. What am i doing wrong? Is sed appropriate for this kind of treatment?

Answer

sat picture sat · Jun 25, 2013

I have edited your sed. Try this,

sed -i.bak 's/-MIB.*$/-MIB/g' ./myfile

Here,

-i - will do the changes in original file and takes the backup of original file in .bak extension