Skip to content
Advertisement

How to make GREP select only numeric values?

I use the df command in a bash script:

df . -B MB | tail -1 | awk {'print $4'} | grep  .[0-9]*

This script returns:

99%

But I need only numbers (to make the next comparison). If I use the grep regex without the dot:

df . -B MB | tail -1 | awk {'print $4'} | grep  .[0-9]*

I receive nothing. How to fix?

Advertisement

Answer

If you try:

 echo "99%" |grep -o '[0-9]*'

It returns:

99

Here’s the details on the -o (or --only-matching flag) works from the grep manual page.

Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line. Output lines use the same delimiters as input, and delimiters are null bytes if -z (–null-data) is also used (see Other Options).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement