2

The below command displays devices with file system usage above 10%. However it is not displaying the column headings. This is the command I use:

df -Ph | awk '+$5 >= 10 {print}'

I want output like this

Filesystem                      Size  Used Avail Use% Mounted on 
/dev/mapper/VolGroup00-LogVol00   9.7G  1.9G  7.4G  21% /
/dev/mapper/VolGroup00-LogVol     5.0G  665M  4.1G  14% /app 
server:/data                      5.0G  546M  4.3G  12% /data
Will
  • 6,561
  • 3
  • 30
  • 41

2 Answers2

4

you can do this using two conditions for awk to print the line: either disk usage is above 10%, either the first parameter is "Filesystem":

df -Ph | awk '(+$5 >= 10 || $1=="Filesystem") {print}'

edit as suggested by Jotne you can write this shorter like this (print is default action):

df -Ph | awk '+$5>=10 || $1=="Filesystem"'
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • You do not need the parentheses and no need the `print`, so it could be done like this: `df -Ph | awk '+$5>=10 || $1=="Filesystem"'` – Jotne Apr 13 '14 at 09:17
3

Print if we are looking at the first line or the fifth field is greater than or equal to 10.

df -Ph | awk 'NR==1 || +$5 >= 10'

Awk's default action is to print the line so block can be omitted.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • +1 that is THE way to do this in awk. I've never seen just `+$5` used to force a numeric comparison, I've always used `($5+0)`. Do you know if that's guaranteed to work and portable? – Ed Morton Apr 13 '14 at 15:19