Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make awk not use scientific notation when printing small values?

Tags:

awk

In the following awk command

 awk '{sum+=$1; ++n} END {avg=sum/n; print "Avg monitoring time = "avg}' file.txt 

what should I change to remove scientific notation output (very small values displayed as 1.5e-05) ?

I was not able to succeed with the OMFT variable.

like image 461
Manuel Selva Avatar asked Oct 03 '12 17:10

Manuel Selva


People also ask

What is awk '{ print $1 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.

What is awk NF?

The “NF” AWK variable is used to print the number of fields in all the lines of any provided file. This built-in variable iterates through all the lines of the file one by one and prints the number of fields separately for each line.


2 Answers

You should use the printf AWK statement. That way you can specify padding, precision, etc. In your case, the %f control letter seems the more appropriate.

like image 117
C2H5OH Avatar answered Oct 05 '22 10:10

C2H5OH


I was not able to succeed with the OMFT variable.

It is actually OFMT (outputformat), so for example:

awk 'BEGIN{OFMT="%f";print 0.000015}' 

will output:

0.000015 

as opposed to:

awk 'BEGIN{print 0.000015}' 

which output:

1.5e-05 

GNU AWK manual says that if you want to be POSIX-compliant it should be floating-point conversion specification.

like image 42
Daweo Avatar answered Oct 05 '22 12:10

Daweo