I want to filter the records where disk usage is greater than 70% using df and awk/if/substr. The problem is it looks like only the first character is considered when comparing:
commands:
# df -Pah | awk '{if(substr($5,1,length($5)-1)>70) print $5}'
Use%
9%
8%
But like this it works:
commands:
# df -Pah | awk '{if(substr($5,1,length($5)-1)-70>0) print $5}'
100%
100%
100%
# df -Pah | awk '{if(substr($5,1,length($5)-1)-30>0) print $5}'
54%
35%
100%
100%
100%
Does anyone know why?
PS: df -Pah
output, in fact it's quit the same as df -h
:
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 2.0G 1020M 894M 54% /
udev 4.0G 420K 4.0G 1% /dev
tmpfs 4.0G 16M 3.9G 1% /dev/shm
df -Pah
. (You may change the names of your devices and mount points if you consider them sensitive, but be sure to keep the layout correct.)awk
condition toif(substr($5,1,length($5)-1)+0>70)
in order to force the result as numeric and see the resultif(substr($5,1,length($5)-1)+0>70)