Search code examples
regexgrepposix

grep(POSIX) regex floating point between blank space in single line


i need regex for grep(posix) to print only(return true) if every floating point separate by blank space in single line is valid like:

3.14 12 18.9

and not print anything (return false) if one or more invalid floating point appear in that line between blank space like:

8.12 1.1.1 78

or

1..28 1.09 46

the floating point can appears as much as possible in that single line, as long is valid floating point separate by blank space it will return true/print by grep(posix).

currently i have regex for grep:

grep -E "^[[:blank:][:digit:]]*.+[[:digit:]]+$" FILE

it work for pattern like:

1.13 1 1.2.3 1. 1

but i don't wont pattern like 1.2.3 and 1. 1 to be match, just i need is 1.13 or 1 between blank space.

note: i just need that work single line only.


Solution

  • You can use

    grep -E '^[0-9]+(\.[0-9]+)?([[:blank:]][0-9]+(\.[0-9]+)?)*$'
    

    Details:

    • ^ - start of string
    • [0-9]+ - one or more digits
    • (\.[0-9]+)? - an optional occurrence of a . and one or more digits
    • ([[:blank:]][0-9]+(\.[0-9]+)?)* - zero or more occurrences of
      • [[:blank:]] - a tab or space
      • [0-9]+(\.[0-9]+)? - one or more digits followed with an optional occurrence of a . and one or more digits
    • $ - end of string.