the 'if' condition check is not working for me when I use awk

576 views awk
5

Have a set of users having disk space in /nobackup. I want to filter users who are using more the 70G of space

INPUT format: Disk space dir/username

INPUT:

351G    /nobackup/dwoo
80G     /nobackup/snjos
73G     /nobackup/ka
67G     /nobackup/mseer
67G     /nobackup/monche
65G     /nobackup/sambut
64G     /nobackup/submee
64G     /nobackup/jushe
64G     /nobackup/dichr
63G     /nobackup/n

Modifying this output using AWK to only print the names if disk space is above 70G

cat /tmp/test | awk '{var=substr($1, 1, length($1)-1); split($2,a,"/"); if($1 > 70) {print a[3]}}'

In output I only get

snjos
ka

Where I should get

dwoo
snjos
ka

answered question

You are experiencing alphabetic comparison, that's why '351' is less than '70'. You want to do a numeric comparison. You might try using the strtonum() function to convert your string field values to a number. e.g. ` if(strtonum($1) > 70)`

2 Answers

5

Because $1 is a string (it contains non-digits), the > operator is a lexical comparison (i.e. dictionary order) where "3" < "7". You need to convert $1 to a number first: put it in a numeric context by adding zero.

if ($1 + 0 > 70) {print a[3]}

posted this
0

You are defining var but never using it. I would recommend letting awk strip the G and split the path by changing the field separator:

 awk '$1 > 70 { print $NF}' FS=[G/] /tmp/test

Note that this approach will incorrectly match a user using 800M, but that's an inherent flaw of you approach which needs a different solution.

posted this

Have an answer?

JD

Please login first before posting an answer.