24

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?

7 Answers 7

51

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

Sign up to request clarification or add additional context in comments.

4 Comments

You can also use the alias \d like echo "99%" |grep -o '\d*'
Are you sure about that @FabSa ? have you done a test? AFAK, grep default use BRE..
It works for me yes (on OsX) but maybe you need the "extended-regexp" (with -E)
@FabSa for gnu grep, to make \d effective, you need -P. And if there is a portable version available, use the portable one.
9

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]+'

1 Comment

This won't work on all versions/builds of grep. You will need -P or -E So this will work echo 'Here is a line mentioning 99% somewhere' | grep -oP '[0-9]+' Or echo 'Here is a line mentioning 99% somewhere' | grep -oP '[0-9]+'
1

How about:

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

Comments

1

No need to used grep here, Try this:

df . -B MB | tail -1 | awk {'print substr($5, 1, length($5)-1)'}

Comments

1
function getPercentUsed() {
    $sys = system("df -h /dev/sda6 --output=pcent | grep -o '[0-9]*'", $val);
    return $val[0];
}

Comments

1

Don't use more commands than necessary, leave away tail, grep and cut. You can do this with only (a simple) awk

PS: giving a block-size en print only de persentage is a bit silly ;-) So leave also away the "-B MB"

df . |awk -F'[multiple field seperators]' '$NF=="Last field must be exactly --> mounted patition" {print $(NF-number from last field)}'

in your case, use:

df . |awk -F'[ %]' '$NF=="/" {print $(NF-2)}'

output: 81

If you want to show the percent symbol, you can leave the -F'[ %]' away and your print field will move 1 field further back

df . |awk '$NF=="/" {print $(NF-1)}'

output: 81%

Comments

0

You can use Perl style regular expressions as well. A digit is just \d then.

grep -Po "\\d+" filename

-P Interpret PATTERNS as Perl-compatible regular expressions (PCREs).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.