0

PROBLEM:

I'm writing a script to configure Debian installs..... I need to comment out the line in sources.list for the DVD, I'm trying to use sed, I know how to delete the line or lines but I want to comment it out instead.

What I have is this:

sed -i '/deb cdrom:[Debian GNU/Linux 9.4.0 _S/s/^/#/' /etc/apt/sources.list

but it gives this error:

sed: -e expression #1, char 44: unterminated address regex

I'm guessing that it's the bracket that is the trouble.

QUESTION:

What is the trouble here and how do you fix the command and/or is there some other way of doing this?

3

Try this,

  sed -i '/deb cdrom:\[Debian GNU\/Linux 9\.4\.0 _S/s/^/#/' /etc/apt/sources.list
  • we need to escape the [, / and . in the search pattern, / because it's the delimiter, and [ and . because they are regexp operators.
0

Since the search string is a constant, with the presence of regex metacharacters being the irritants, you could also do it as follows:

 awk -i inplace 'index($0, "deb cdrom:[Debian GNU/Linux 9.4.0 _S"){$0 = "#" $0}1' /etc/apt/sources.list

You're in luck if you have access to GNU awk 4.1.0 or above for the in-place editing effect. OTW, you may do with Perl:

perl -pi -e 's/^/#/ if index($_, "deb cdrom:[Debian GNU/Linux 9.4.0 _S") + 1' /etc/apt/sources.list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.