Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

test on decimal value + awk

SOLVED
Go to solution
Highlighted
MR VILLOT MR MONTAGNE
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
Peter Godron
Honored Contributor

Re: test on decimal value + awk

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

Re: test on decimal value + awk

Laurent,
sorry, my previous was wrong answer.

Please replace the $PCPU with PCPU in the if statement.
Regards
MR VILLOT MR MONTAGNE
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
Peter Godron
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
Jdamian
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