Cut / grep and df -h

cutgrepscripting

How can I grep or cut the "173G" under "Verf"?

I need this for Unix scripting in school.

jonas@jonaspc:~/$ df -h /dev/sda2
Dateisystem    Größe Benutzt Verf. Verw% Eingehängt auf
/dev/sda2       293G    121G  173G   42% /media/Windows

Best Answer

The most comfortable solution for such task is awk:

df -h /dev/sda2 | awk 'NR==2{print$4}'

Or if more partitions are listed, you can pick the right line by the mount point:

df -h | awk '$1=="/dev/sda2"{print$4}'

Is also simple with sed, but less nice if you need to debug it a few mounts later :

df -h /dev/sda2 | sed -rn '2s/^((\S+)\s+){4}.*/\2/p'

df -h | sed -rn '/^\/dev\/sda2/s/^((\S+)\s+){4}.*/\2/p'

That supposes GNU sed. POSIX compatible syntax includes many escaping:

df -h /dev/sda2 | sed -n '2s/^\(\(\S\+\)\s\+\)\{4\}.*/\2/p'

df -h | sed -n '/^\/dev\/sda2/s/^\(\(\S\+\)\s\+\)\{4\}.*/\2/p'