Replace Strings Using Sed And Regex Replace Strings Using Sed And Regex bash bash

Replace Strings Using Sed And Regex


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.


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

should do it.


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

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