Get part of a line

Hi,
I run this:

 df -g  | grep ddxx

And I get:

/dev/lvrec   105.00             59.02      44%   734099     5% /u01/appl_top/ddxx

How can I get percent used (here 44) in a variable?

I run:

df -g  | grep ddxx | awk '{ print $4 }'

But I get:

44%

How can I delete % symbole?

Thanks.

Try the following

df -g | awk '/ddxx/ { print $4+0 }'

The +0 casts the $4 to a number; many awk versions then ignore the trailing %

3 Likes

Yes.
Really thank you.
It works .
Regards.

With your shown samples and attempts, you could also give it a try to following solution also. It will need GNU grep.

df -g | grep '/ddxx$' | grep -oP '^(\S+\s+){3}\K\d+'

Basically using \K option to forget all matched parts(in regex) and then print only matched parts which comes after \K.

Thanks,
R. Singh

1 Like

IMHO the real master of regex is perl.
The following prints the digits before the (first) %

perl -lne '/(\d+)%/ and print $1'
3 Likes

That's a nice one @MadeInGermany :+1:
Yes mentioned grep code also uses PCRE regex which is again Perl's regex :slight_smile: cheers.

1 Like