to be more clear, I need to get the used space percentage (only the percentage without the other content in the line< of VOL (line 7) Using grep command*
12 Answers
Use grep with lookahead. It looks for one or more number followed by a % and only prints the number.
grep -Po "[0-9]+(?=%)" -P: Perl-compatible regular expression so we can use lookahead.-o: Only print the matched parts.
$ echo '/dev/sda3 22G 4.1G 17G 20% /vol/store' | grep -Po "[0-9]+(?=%)" 20 Another possibility:
echo "/dev/sda3 22G 4.1G 17G 20% /vol/store" | grep -o "[[:digit:]]*%" 20% Without the % sign:
echo "/dev/sda3 22G 4.1G 17G 20% /vol/store" | grep -Po "[[:digit:]]*(?=%)" 20