M BUZZ CRAZE NEWS
// general

how do I grep specific pattern from a Line using grep command?

By Mia Morrison

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*

1

2 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy