How to make GREP select only numeric values?

grep will print any lines matching the pattern you provide. If you only want to print the part of the line that matches the pattern, you can pass the -o option:

-o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Like this:

echo 'Here is a line mentioning 99% somewhere' | grep -o '[0-9]+'

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).


How about:

df . -B MB | tail -1 | awk {'print $4'} | cut -d'%' -f1

Tags:

Linux

Bash

Grep