31

I'm trying to uncomment file content using sed but with regex (for example: [0-9]{1,5})

# one two 12
# three four 34
# five six 56

The following is working:

sed -e 's/# one two 12/one two 12/g' /file

However, what I would like is to use regex pattern to replace all matches without entering numbers but keep the numbers in the result.

6
  • 1
    How do you define "matches"? Which lines do you want to uncomment and how do you decide that?
    – Lee Meador
    Commented Dec 28, 2012 at 16:33
  • it should only uncomment text witch contains numbers.
    – tangi
    Commented Dec 28, 2012 at 16:42
  • this is an old question. But it helped me understand sed. I have made a significant change tot he Highest score answer (edits in review) and ask if @starryknight64 please review and choose an answer.
    – dank8
    Commented May 19, 2023 at 3:12
  • @dank8 I can't select an answer as I'm not the author of this question, I've only edited it for formatting. tangi is though. Commented May 21, 2023 at 2:11
  • @starryknight64 oops sorry my bad.
    – dank8
    Commented May 22, 2023 at 0:56

6 Answers 6

28

For complying sample question, simply

sed 's/^# //' file

will suffice, but if there is a need to remove the comment only on some lines containing a particular regex, then you could use conditionnal address:

sed '/regex/s/^# //' file

So every lines containing regex will be uncomented (if line begin with a #)

... where regex could be [0-9] as:

sed '/[0-9]/s/^# //' file

will remove # at begin of every lines containing a number, or

sed '/[0-9]/s/^# \?//' file

to make first space not needed: #one two 12, or even

sed '/[0-9]$/s/^# //' file

will remove # at begin of lines containing a number as last character. Then

sed '/12$/s/^# //' file

will remove # at begin of lines ended by 12. Or

sed '/\b\(two\|three\)\b/s/^# //' file

will remove # at begin of lines containing word two or three.

1
  • 2
    I like this; it's a bit clearer about the intent. (You don't want to "replace # + some stuff + a digit + other stuff, with everything but the #"; you want to "remove the # on lines that contain digits".) For regex, you could say [0-9] to have it only affect lines with digits in them, or [0-9]$ if the number has to be at the end.
    – cHao
    Commented Dec 28, 2012 at 21:17
9
sed -e 's/^#\s*\(.*[0-9].*\)$/\1/g' filename

should do it.

3
  • the issue here is that this will uncomment everything including text without numbers.
    – tangi
    Commented Dec 28, 2012 at 16:43
  • Could by written: sed -e '/[0-9]/s/^# *//' filename! See my answer Commented Oct 4, 2019 at 16:50
  • Don't forget to add -i to replace in place, directly in the file, but without that just to check the output
    – Seraf
    Commented Aug 23, 2020 at 16:43
2

Is the -i option for replacement in the respective file not necessary? I get to remove leading # by using the following:

sed -i "s/^# \(.*\)/\1/g" file

In order to uncomment only those commented lines that end on a sequence of at least one digit, I'd use it like this:

sed -i "s/^# \(.*[[:digit:]]\+$\)/\1/g" file

This solution requires commented lines to begin with one space character (right behind the #), but that should be easy to adjust if not applicable.

1

If you only want those lines uncommented which contain numbers, you can use this:

sed -e 's/^#\s*\(.*[0-9]+.*\)/\1/g' file
0

The following sed command will uncomment lines containing numbers:

sed 's/^#\s*\(.*[0-9]\+.*$\)/\1/g' file
-1

I find it. thanks to all of you

echo "# one two 12" | grep "[0-9]" | sed 's/# //g'

or

cat file | grep "[0-9]" | sed 's/# //g'
1
  • These solutions delete lines which don't contain digits but that were not already uncommented. Maybe there are no such lines (not even any blank lines?), but it arguably isn't wholly appropriate behaviour. Commented Dec 21, 2018 at 22:46

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