Alex Rivera | Logout

Replace Strings Using Sed And Regex

Asked 2012-12-28T16:30:00.900
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.

Edit
Report

1 Answer

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.

answered 2012-12-28T20:55:46.377

Your Answer