How can I extract data from a command output with Bash?

6,064
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}'
Share:
6,064

Related videos on Youtube

Randall
Author by

Randall

Updated on September 18, 2022

Comments

  • Randall
    Randall over 1 year

    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.

    • Stéphane Chazelas
      Stéphane Chazelas over 6 years
      And what will you do when it goes up to 0.921TB? It seems to me that the GB part is important here.