BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

test on decimal value + awk

SOLVED
Go to Solution
Highlighted
Frequent Advisor

test on decimal value + awk

Hi,

I did a script to get top process with high CPU. I want to do a test with value 1.0 but I do not know how to do correctly the test :

awk '
BEGIN {
printf("PID\tUser\tTime\tCPU\tCommand\t\n")
}

BEGIN {
OFMT="%.6g"
PCPU=1.0
while ("cat /tmp/topfile" | getline )
{if ($12 >= $PCPU)
{OFS="\t"; print $3,$4,$10,$12,$13}
}
}

but I get :
PID User Time CPU Command
20333 appe11t 187:16 3.61 java
16006 root 0:23 0.93 top
25825 appe11t 152:30 0.30 java
21858 orae11t 0:00 0.76 oracleE11T
21558 appe11t 10:21 0.48 java
19392 appp11t 14:32 0.33 java
19390 appp11t 18:41 0.65 java
18558 orae11t 0:00 1.66 oracleE11T

thanks for help

Laurent
5 REPLIES
Honored Contributor

Re: test on decimal value + awk

Laurent,
have tried replacing >= with -ge
Regards
Honored Contributor

Re: test on decimal value + awk

Laurent,
sorry, my previous was wrong answer.

Please replace the $PCPU with PCPU in the if statement.
Regards
Frequent Advisor

Re: test on decimal value + awk

Peter,

what is the difference between $PCPU and PCPU in my condition ?
test on character value and numeric value.

Thanks
Laurent
Honored Contributor

Re: test on decimal value + awk

Laurent,
it might help with testing to add before the if statement:

OFS="|";print $12,$PCPU,PCPU

You can then see the $PCPU has null value, whereas PCPU has the correct value.

Also be aware that you are comparing numbers, so 1.0 becomes 1 for the comparisons.

Hope this makes it clearer.

Regards
Respected Contributor

Re: test on decimal value + awk

I agree with Peter. The following line is wrong

$12 >= $PCPU

Right one should be:

$12 >= PCPU

My own script should be:

awk '
BEGIN {
printf("PID\tUser\tTime\tCPU\tCommand\n")
OFMT="%.6g"
PCPU=1.0
OFS="\t"
}

$12 >= PCPU { print $3,$4,$10,$12,$13 }
' /tmp/topfile