user25917 user25917 - 5 months ago 13
Bash Question

Used OFMT in awk

I'm reading UNIX Shells by Example / 3th Edition

Example 5.7 in the book

nawk 'BEGIN{OFMT="%2.f"; print 1.2456789, 12E-2}'


I want to receive:

1.25 0.12


but I get:

1 0

Answer

Use (notice change from %2.f to %.2 - 2 digits after the dot):

nawk 'BEGIN{OFMT="%.2f"; print 1.2456789, 12E-2}'

If you want to get:

1.24 0.12