Taking average of decimal number using bash
How can I take average of decimal values开发者_如何学编程 written in a file: TestFile as
Time to write: 0.000118000 sec
Time to write: 0.000119000 sec
Time to write: 0.000122000 sec
Wrong Soln:
Following prints just zero i.e. 0
awk '{sum+=$7}END{print sum/NR}' TestFile
This solution will target calculation at the correct lines:
awk '/Time to write/ {sum+=$4; count++} END {print "avg:", sum/count}' data.txt
EDIT
since you are having trouble, and seem to return zero try using this
grep -oP "\d+\.\d+" testFile | awk -vx=0 '{x += $1} END {print x/NR}'
this will work if the file is double spaced or not. it only prints the match of a file that has as decimal number. and sends it to awk.
the -P flag is a perl regular expression. you could do the same with -E, but \d+
matches one or more digits, \.
matches a period. a . has special meaning in regular expressions and need to be escaped and \d+
matches one or more digits so put that together, '\d+\.\d+'
, you have a decimal.
lastly, if you continue to get scientific notation you may consider printf to achieve floating point noation
awk -vx=0 '{x += $4} END { printf ("%8.9f", x/NR) } testFile'
you can specifiy something smaller like "%4.3f" to print only 4 numbers after a decimial, Conversely, use %e to print in scientific notation
More using printf in awk
old information, see above <hr>
awk -vx=0 '{x += $4} END {print x/NR}' testFile
which outputs:
0.000119667
for each line append $4, which in your test file is the number, to x. At the end divide x for number of lines.
if your file is really double spaced run the following first:
sed -i '/^$/d' testFile
to remove blank lines. you may want to consider not editing testFile in place by removing -i and doing something like this sed '/^$/d' testFile > newFile
or even combine the two files and pipe stdout from sed
to awk
sed '/^$/d' testFile | awk -vx=0 '{x += $4} END {print x/NR}'
if this returns 0, you may have a problem with your testFile.
精彩评论