Grep -o

Attempting to use

egrep -o

to return on a specific numerical value which would be two digits a dot then one digit (ex 25.2 or 4.3) Trying to use this format:

egrep -o '[[:digit:]].[[:digit:]]|[[:digit:]][[:digit:]].[[:digit:]]'

But it's returning unexpected results. Any help would be appreciated.

Try:

egrep -o '[[:digit:]]\.[[:digit:]]|[[:digit:]][[:digit:]]\.[[:digit:]]'

To grep for literal dots.

This can be simplified to the equivalent:

grep -Eo '[[:digit:]]{1,2}\.[[:digit:]]'

But note that it will also return parts of numbers that do not conform to this format.

printf "%s\n" 1.2 1.12 22.2 333.3 | 
egrep -o '[[:digit:]]\.[[:digit:]]|[[:digit:]][[:digit:]]\.[[:digit:]]'
1.2
1.1
22.2
33.3

So instead try word boundary symbols:

grep -Eo '\<[[:digit:]]{1,2}\.[[:digit:]]\>'

Which produces:

1.2
22.2
1 Like

Yes! Thank you Scrutinizer!