Linux – Replace a block of numbers in sed

find and replacelinuxregexsed

I've been fiddling with this for a while now and can't seem to figure it out.
What I'm trying to do is replace all numbers in a file with a single #.

Sounds simple, and it should be, but I can't get my head around it. Any help would be appreciated.

What I've got so far (but doesn't work) is:

echo "fdsafdsa 32432 dsafdas" | sed 's/[0-9]+/#/g'

The output I expect is:

fdsafdsa # dsafdas

But sed gives me the same string with nothing replaced.

Any clues?

Best Answer

You don't need the +. Just use the following:

echo "fdsafdsa 32432 dsafdas" | sed 's/[0-9]/#/g'

[0-9] will already match all digits, and replace every single one with #.


Since + is extended syntax, you could also do:

echo "fdsafdsa 32432 dsafdas" | sed -E 's/[0-9]+/#/g'

to replace the whole block of digits with one #.