0

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
5
  • Please don't post screenshots of text. Copy the text here and apply code formatting.
    – muru
    Commented Sep 14, 2017 at 2:26
  • And show us the output of 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.) Commented Sep 14, 2017 at 2:29
  • it should work in both, what OS are you using and awk version? change the awk condition to if(substr($5,1,length($5)-1)+0>70) in order to force the result as numeric and see the result Commented Sep 14, 2017 at 3:20
  • @αғsнιη this happened on CentOS7 and SuSE11.3 ,
    – Rorschach
    Commented Sep 14, 2017 at 3:37
  • but you didn't tell us if this doest works or not?! if(substr($5,1,length($5)-1)+0>70) Commented Sep 14, 2017 at 3:48

1 Answer 1

0

Awk comparisons are as I recall either numeric or string comparisons, depending on the type of the left operand, and that would explain what you're seeing: substr gives a string result, and strings beginning 'U', '9', and '8' are all greater than any string beginning '7'.

Subtraction produces a numeric result.

2
  • No either if it's comparing first character it should not give result print $5 only one num character length result! Commented Sep 14, 2017 at 3:44
  • sorry,i just copy the first 2 line.
    – Rorschach
    Commented Sep 28, 2017 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .