Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncoment lines using bash script

Tags:

bash

# deb http://archive.canonical.com/ubuntu lucid partner
# deb-src http://archive.canonical.com/ubuntu lucid partner

above lines from /etc/apt/sources.list.There are number of lines. How to uncomment above 2 lines with bash script.

like image 918
Venkat Avatar asked Oct 13 '11 07:10

Venkat


3 Answers

I'd say

 sed -e "s/^# deb/deb/g" /etc/apt/sources.list

Instead of

 sed -e "s/^# //g" /etc/apt/sources.list

because th second sed command will either uncomment lines such :

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
like image 163
m0ntassar Avatar answered Nov 08 '22 03:11

m0ntassar


The accepted answer from @m0ntassar works, but it will uncomment all lines that begin with # deb which might open up access to repositories one doesn’t want—or need—access to.

Instead I would recommend targeting lines that are:

  1. Lines that begin with deb but are commented out with a # like this: # deb.
  2. And lines that specifically end with partner.

So my suggested Sed command would be as follows:

sed -e "/^#.*deb.*partner$/s/^# //g" /etc/apt/sources.list

That command with the -e command would show you the output, but the -i flag would edit the file in place:

sudo sed -i "/^#.*deb.*partner$/s/^# //g" /etc/apt/sources.list

Running that as sudo in this example since editing in place would require sudo rights.

like image 39
Giacomo1968 Avatar answered Nov 08 '22 03:11

Giacomo1968


You can use sed to replace the #

 sed -e "s/^# //g" /etc/apt/sources.list
like image 1
ajreal Avatar answered Nov 08 '22 03:11

ajreal