see : https://www.gnu.org/software/gawk/manual/html_node/Printf.html#Printf
{ print $1 $2 } # will concatenate the referenced fields into a single string.
{ print $1, $2 } # will display the listed field using the OFS, output field separator.
awk provides a version of the c-type printf for a more controlled printing facility.
printf ( format , argument[s] );
{ printf ( "%6.3f\n", $2 ); }
martinets 1 Lorrie's 2.2 pooched 3.33 excoriation 44.44 pries 55.555 caravan 66.6666 handballs 77.77777 slap 88.888888 specially 12 woeful 1212 added 12.1212 rocky a1a1 door a1a2 |
awk '{ printf ( "%6.3f\n", $2 ); }' data
Output would be :
1.000 2.200 3.330 44.440 55.555 66.667 77.778 88.889 12.000 1212.000 12.121 0.000 0.000 |
The . counts as one of the characters.
printf attempts to align the decimal point. If the integer portion is larger than the format specified, alignment is skewed, but all digits displayed.
The fractional portion will be rounded to fit 3 characters and all digits displayed.
The following also prints the topic field, $1
awk '{ printf ( "%10s %6.3f\n", $1, $2 ); }' data
martinets 1.000 Lorrie's 2.200 booched 3.330 excoriation 44.440 pries 55.555 caravan 66.667 handballs 77.778 slap 88.889 specially 12.000 woeful 1212.000 added 12.121 Added 12.121 rocky 0.000 boor 0.000 |
There are several 'issues' with this, text is right justified, several of the floats are still not aligned.
The -, minus, can be used to left justify the text.
And the default size for both data fields can be increased.
awk '{ printf ( "%-14s %8.3f\n", $1, $2 ); }' data
Output would be :
martinets 1.000 Lorrie's 2.200 pooched 3.330 excoriation 44.440 pries 55.555 caravan 66.667 handballs 77.778 slap 88.889 specially 12.000 woeful 1212.000 added 12.121 rocky 0.000 door 0.000 |
Some additional observations: