Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been struggling with "grep -Po" off and on for a couple hours.

What I would like to do is to find out how many bytes the virtual disk size is as reported by qemu-img info.

Here's example output:

$ qemu-img info 022eb199-954d-4c78-8550-0ea1d31111ec                                           
image: 022eb199-954d-4c78-8550-0ea1d31111ec
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 20G
cluster_size: 65536

And here's what I've been trying variations of:

$ qemu-img info 022eb199-954d-4c78-8550-0ea1d31111ec | grep -Po '\(([0-9]+).*\)'
(42949672960 bytes)

But I would just like the number. I guess I'm struggling with how to tell "grep -Po" which part of the regex I'd like extracted. I thought the "(" brackets would do that.

Any help would be greatly appreciated. :)

share|improve this question
How about awk? cat text | awk '/^virtual size/ {gsub(/.*\(/,""); gsub(/ bytes.*/,"");print}'. Find the line matching 'virtual size', replace everything before the '(', replace everything after the ' bytes). – Zoredache Jun 4 '13 at 21:53

You can use cut twice, for example:

echo "virtual size: 40G (42949672960 bytes)"| cut -f 4 -d" "| cut -c 2-

First cut cuts (42949672960,-f4 fourth field, -d" " is the separator. The second cut cuts 42949672960 -c2- (cuts from second character to the end.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.