Search code examples
bashexceptionsedreplacenumbers

How to sed all points except decimal ones


I'm trying to remove all points in a file except the one used for decimals

Example : Hello everyone. How. to sed 4.32 and 4. ?

Should be replaced by : Hello everyone How to sed 4.32 and 4. ?

I tried this : sed 's/[^0-9]\.[^0-9]//g' but it's not working properly

Result : Hello everyonHoto sed 4.32 and 4. ?

and this sed '/[0-9].[0-9]/ ! s/\.//g' but it doesn't work either. Its not removing any '.' : Hello everyone. How. to sed 4.32 and 4. ?


Solution

  • You can use:

    sed -E 's/([^0-9])\.([^0-9])/\1\2/g'
    

    which matches any non digit char, followed by a literal . and a non digit char, and replaces it with the two non digit characters.