Re: test on decimal value + awk (79 Views)
Reply
Frequent Advisor
MR VILLOT   MR MONTAGNE
Posts: 64
Registered: ‎11-19-1998
Message 1 of 6 (79 Views)
Accepted Solution

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
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 2 of 6 (79 Views)

Re: test on decimal value + awk

Laurent,
have tried replacing >= with -ge
Regards
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 3 of 6 (79 Views)

Re: test on decimal value + awk

Laurent,
sorry, my previous was wrong answer.

Please replace the $PCPU with PCPU in the if statement.
Regards
Please use plain text.
Frequent Advisor
MR VILLOT   MR MONTAGNE
Posts: 64
Registered: ‎11-19-1998
Message 4 of 6 (79 Views)

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
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 5 of 6 (79 Views)

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
Please use plain text.
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 6 of 6 (79 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation