3

I'm having difficulty extracting the Filesystem and Mounted on columns from the df ouput.

$df -Hl

Results in

Filesystem                          Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk1                          250G   226G    24G    91% 1576021 4293391258    0%   /
localhost:/BJeZ62GypQBU7sxaZg0qP6   250G   250G     0B   100%       0          0  100%   /Volumes/MobileBackups
/dev/disk2s1                         16G   4.7G    11G    30%       0          0  100%   /Volumes/NO NAME
/dev/disk3s1                         15G   8.0G   7.5G    52%       0          0  100%   /Volumes/FLASH DRIVE

Adding column selection

df -Hl | awk '{ print $1"\t"$9 }'

results in

Filesystem  Mounted
/dev/disk1  /
localhost:/BJeZ62GypQBU7sxaZg0qP6   /Volumes/MobileBackups
/dev/disk2s1    /Volumes/NO
/dev/disk3s1    /Volumes/FLASH

My Mounted on column has spaces in the file name which is throwing off results. (ie I get "NO" instead of "NO NAME") How do I get the columns to include the entire mount name?

Dan
  • 31

1 Answers1

0

Since the field with spaces is the last part of the line, you could just grab all the fields from 9 to the end.

df -Hl | awk '{ mounted=$9; 
                for (i = 10; i <= NF; i++) mounted = mounted " " $i;
                print $1 "\t" mounted }'

This will work as long as the parts of the mount point name are just separated by single spaces -- multiple spaces and/or tabs will be compressed into a single space.

And none of the other fields can have spaces in them.

Barmar
  • 2,545