17

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Youri_Margarine
  • 173
  • 1
  • 1
  • 6

3 Answers3

7

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

sat
  • 14,589
  • 7
  • 46
  • 65
2

You need to leave out the -n option. Check out http://www.gnu.org/software/sed/manual/sed.html#index-g_t_002d_002dquiet-7

ctn
  • 2,887
  • 13
  • 23
2

sed -n option suppresses the default printing which is later enabled if you put p option after your sed statement.

In your example above, what you are telling sed is to suppress printing of all lines and then print only those lines where substitution has been done successfully. As a result the line where no substitution is made is never printed.

You need remove the -n option and p from your command:

sed -ie 's/-MIB.*$/-MIB/' ./myfile
jaypal singh
  • 74,723
  • 23
  • 102
  • 147