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*
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 More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"