The cut
command considers each and every delimiter to begin a new field; if you told it to delimit the output based on spaces and tabs, every space creates a new field. That's why you got unexpected output.
Printing field 5 could be done more directly with awk
, which combines any number of sequential spaces into one delimiter: df -Pg | awk '{print $5}'
. You still have two problems to solve, though: skipping the header and considering filesystems that have a space in their mount-point. You can skip the header fairly easily, since it's the first line of output: df -Pg | awk 'NR > 1 {print $5}'
, but dealing with the remaining fields takes more work.
I would propose that you use different options for the df
command to request colon-separated output with specific fields: df -c -F %z %m
. The -F
option automatically includes the filesystem device and block fields, so you'd want to skip those. We still want to skip the header, so we end up with:
df -c -F %z %m | sed 1d | cut -d: -f3,4
... to delete the first line, then output only the third and fourth colon-delimited fields. That should give your monitoring program the relevant information. Depending on what actions you're taking, you may want to include field 1 (the device) if you need to check the free volume group space. Be aware that the df
output adds a space to the beginning of the mount-point field (which may be a side effect of requesting colon-separated output and its parsing of the -F argument). Be aware also that you may see network-mounted filesystems; if you only want to see local filesystems, add the -T local
option:
df -T local -c -F %z %m | sed 1d | cut -d: -f3,4