How to remove one line from repository with sed

Solution 1:

You can use any* symbol to delimit the search/replace strings which does not conflict with symbols in the strings, as long as you use it consistently, e.g.:

sudo sed 's#deb http://downloads.sourceforge.net/project/xenlism-wildfire/repo deb/##g' /etc/apt/sources.list

This reduces the need to escape a problematic character (which could be done using \ as an escape).

*I am sure there are exceptions, especially if using older versions of sed, but I'm not aware of any limitations off hand.

Solution 2:

Run like this:

sudo sed -i '\%^deb http://downloads.sourceforge.net/project/xenlism-wildfire/repo deb%d' /etc/apt/sources.list

The problems I fixed:

  • Use -i flag to modify the file in-place. Without that, the command would just print the output of sed to the screen, without updating the file.
  • Instead of using s/// to replace the line with empty string, I used the d command to delete the line.
  • For the pattern matching, instead of /.../, I used \%...%, because % doesn't appear in the pattern. With /, all occurrences of / would have to be replaced.

Note that possibly a simpler filter might be good enough. For example if you know that xenlist-wildfire is unique in the file, then this simpler command will work too:

sudo sed -i '/xenlism-wildfire/d' /etc/apt/sources.list

Since in this example there is no /, I could use the simpler /.../ filter.

(Thanks for @steeldriver for the improvement ideas.)