Extracting positive/negative floating-point numbers from a string

numeric datasedtext processing

I am trying to extract numbers out of some text. Currently I am using the following:

echo "2.5 test. test -50.8" | tr '\n' ' ' | sed -e 's/[^0-9.]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' '

This would give me 2.5, "." and 50.8. How should I modify the first sed so it would detect float numbers, both positive and negative?

Best Answer

grep works well for this:

$ echo "2.5 test. test -50.8" | grep -Eo '[+-]?[0-9]+([.][0-9]+)?'
2.5
-50.8

How it works

  • -E

    Use extended regex.

  • -o

    Return only the matches, not the context

  • [+-]?[0-9]+([.][0-9]+)?+

    Match numbers which are identified as:

    • [+-]?

      An optional leading sign

    • [0-9]+

      One or more numbers

    • ([.][0-9]+)?

      An optional period followed by one or more numbers.

Getting the output on one line

$ echo "2.5 test. test -50.8" | grep -Eo '[+-]?[0-9]+([.][0-9]+)?' | tr '\n' ' '; echo ""
2.5 -50.8
Related Question