1

I'm new to Linux and I'd like to get the "DRIVE GB" from this output:

[randall@home gdrive]$ drive quota 
Name: Randall D
Account type:   UNLIMITED
Bytes Used: 290959662516         (270.98GB)
Bytes Free: 10995116277760       (10.00TB)
Bytes InTrash:  0                    (0.00B)
Total Bytes:    11286075940276       (10.26TB)

* Space used by Google Services *
Service                              Bytes                               
DRIVE                                270.98GB                            
PHOTOS                               0.00B                               
GMAIL                                0.00B                               
Space used by all Google Apps        270.98GB 

I want to get the "270.38" from DRIVE 270.98GB. I tried with sed reading a few posts here but couldn’t get it.

  • 2
    And what will you do when it goes up to 0.921TB? It seems to me that the GB part is important here. – Stéphane Chazelas Dec 5 '17 at 20:37
3
drive quota | sed -n 's/^DRIVE[[:blank:]]\{1,\}\([0-9.]\{1,\}\).*/\1/p'

Would extract the sequences of 1 or more decimal digits or . following a sequence of 1 or more blanks following DRIVE itself at the start of the line.

If your sed supports the -E option, it would be prettier with:

drive quota | sed -En 's/^DRIVE[[:blank:]]+([0-9.]+).*/\1/p'

Though then, you might as well use perl:

drive quota | perl -lne 'print $1 if /^DRIVE\h+([\d.]+)/'

With GNU grep, when built with (recent for \K) PCRE support:

drive quota | grep -Po '^DRIVE\h+\K[\d.]+'

With awk, you can also do:

drive quota | awk '$1 == "DRIVE" {print 0+$2}'

Your Answer

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

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