Approximate a % value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Approximate a % value
# 1  
Old 08-03-2012
Approximate a % value

Dear community,
I have a log like this:
Code:
02:08:2012:15:00|machine1|    1543523|        234|     1658097|    230529                            
02:08:2012:15:00|machine2|    1415015|        193|     1530636|    210628                            
02:08:2012:16:00|machine3|    1282335|        244|     1407936|    199936                            
02:08:2012:16:00|machine1|    1427341|        198|     1550801|    218483                            
02:08:2012:16:00|machine2|    1256311|          0|     1366949|    176091

Now I calculated the % between 3rd and 4th fields, so I used the following::

Code:
cat server.log |   sed -e "s/\|//g" | awk '$7=100-($3*100)/($3+$4) {print $1,$2,$7"|",$3,$4,$5,$6}'

...and the response is:
Code:
02:08:2012:15:00|machine1| 1543523| 99.9859| 234| 1658097| 230529 
02:08:2012:15:00|machine2| 1415015| 99.9874| 193| 1530636| 210628 
02:08:2012:16:00|machine3| 1282335| 99.9827| 244| 1407936| 199936 
02:08:2012:16:00|machine1| 1427341| 99.9872| 198| 1550801| 218483 
02:08:2012:16:00|machine2| 1256311| 100| 0| 1366949| 176091

And that's should be ok.

What I need now, is approximate the output % (the variable $7) in something like:
Code:
99.988   => 99.988 
99.9894 => 99.989
100      => 100
99.9      => 99.900

Please help, I'm scratching my head to find a solution.! Smilie
# 2  
Old 08-03-2012
Change your print to be printf.

i.e.

Code:
$ echo 99.1459 | awk '{printf "%.3f\n", $1}'
99.146

(although that will also give you 100.000, so if not printf, you may need to use a function to trim the output instead)
This User Gave Thanks to Scott For This Post:
# 3  
Old 08-03-2012
I'll give you a hint Smilie :
Code:
cat test
99.988
99.9894
100
99.9
7.8769

awk '{print $0+0}' OFMT='%.3f' test
99.988
99.989
100
99.900
7.877

These 2 Users Gave Thanks to elixir_sinari For This Post:
# 4  
Old 08-03-2012
Thank you both!!!
Could you please advice me on how integrate the following:
Code:
awk '{print $0+0}' OFMT='%.3f' test

Into my main line?

Thanks!
# 5  
Old 08-03-2012
Using the input you specified with the command line you specified produces the output:
Code:
02:08:2012:15:00machine1 1543523 99.9859| 234 1658097 230529 
02:08:2012:15:00machine2 1415015 99.9874| 193 1530636 210628 
02:08:2012:16:00machine3 1282335 99.9827| 244 1407936 199936 
02:08:2012:16:00machine1 1427341 99.9872| 198 1550801 218483 
02:08:2012:16:00machine2 1256311 100| 0 1366949 176091

which is nothing at all like the output you said this command line produced.

To get the output you said was produced, you'd need to have run something like:
Code:
awk 'BEGIN {OFMT="%.4f"
		FS="|"
		OFS="| "}
	{$7=100-($4*100)/($4+$5)
	printf "%s|",$1
	print $2,$3+0,$7,$4+0,$5+0,$6+0}' server.log

except that most of the lines in your expected output seem to have a trailing space character which this command does not duplicate.
To get the output you seem to want, you need to change the "%.4f" to "%.3f" when setting OFMT.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-03-2012
Thank you it works! Smilie

BTW, sometime I have log like this:
Code:
17:07:2012:00:00|machine1|          0|          0|           0|         0                             
17:07:2012:03:00|machine2|          0|          0|           0|         0

And AWK stops its "analysis" with the following error:
Code:
awk: cmd. line:3: (FILENAME=server.log FNR=355) fatal: division by zero attempted

Is there anything I can do to avoid that?
# 7  
Old 08-03-2012
If you just want it to ignore those lines:

Code:
awk 'BEGIN {OFMT="%.4f"
		FS="|"
		OFS="| "}
	($4+$5) {$7=100-($4*100)/($4+$5)
	printf "%s|",$1
	print $2,$3+0,$7,$4+0,$5+0,$6+0}' server.log

It will run the code block whenever $4+$5 is nonzero, but when they're zero, it won't bother trying (hence won't divide by zero).
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question