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
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
awkold information, see above
<hr>awk -vx=0 '{x += $4} END {print x/NR}' testFilewhich outputs:
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' testFileto remove blank lines. you may want to consider not editing testFile in place by removing -i and doing something like thissed '/^$/d' testFile > newFileor even combine the two files and pipe stdout from
sedtoawksed '/^$/d' testFile | awk -vx=0 '{x += $4} END {print x/NR}'if this returns 0, you may have a problem with your testFile.