Combining awk printf and print strftime command

I have a lines like below, captured from rrdtool fetch command,

1395295200 2.0629986254e+06 7.4634784967e+05
1395297000 2.0198121616e+06 6.8658888903e+05
1395298800 1.8787141122e+06 6.7482866452e+05
1395300600 1.7586118678e+06 6.7867977653e+05
1395302400 1.8222762151e+06 7.1301678859e+05

I'm able to convert the first field from epoch time format to "normal date" format and also separate the field with comma with awk command (directly from the rrdtool fetch command) :

/usr/bin/rrdtool fetch rrdfiles.rrd AVERAGE -r 3600 -e "Mar 20 2014" -s e-14d | cut -f1-2 -d":" |\
 sed 's/://g' | sed '/-nan/d' | awk '{print strftime("%c",$1)","($2)","($3)}'
Thu 20 Mar 2014 01:00:00 PM WIT,2.0629986254e+06,7.4634784967e+05
Thu 20 Mar 2014 01:30:00 PM WIT,2.0198121616e+06,6.8658888903e+05
Thu 20 Mar 2014 02:00:00 PM WIT,1.8787141122e+06,6.7482866452e+05
Thu 20 Mar 2014 02:30:00 PM WIT,1.7586118678e+06,6.7867977653e+05
Thu 20 Mar 2014 03:00:00 PM WIT,1.8222762151e+06,7.1301678859e+05

What I want to achieve is how to format the second and third field to decimal format with awk printf command :

printf "%12.2f\n"

The final output should be :

Thu 20 Mar 2014 01:00:00 PM WIT,2062998.6254,746347.8497
Thu 20 Mar 2014 01:30:00 PM WIT,2019812.1616,686588.8890
Thu 20 Mar 2014 02:00:00 PM WIT,1878714.1122,674828.6645
Thu 20 Mar 2014 02:30:00 PM WIT,1758611.8678,678679.7765
Thu 20 Mar 2014 03:00:00 PM WIT,1822276.2151,713016.7886

How to combine print strftime and printf command in one line ? . Sorry I have try several combination but not work.
Or maybe there are other solution.

TIA.

1 Like
awk '{printf("%s,%12.4f,%12.4f\n",strftime("%c",$1),$2,$3) } '
1 Like

Another way to do it is by changing:

awk '{print strftime("%c",$1)","($2)","($3)}'

to:

awk -v OFMT='%.4f' '{print strftime("%c",$1)","$2+0","$3+0}'

Note that I used %.4f rather than the %12.2f you specified because you showed that you want 4 digits after the decimal point in your output (not 2), and there is no need to force leading spaces when printing smaller values in a CSV formatted output file so the minimum field width 12 is not needed.

1 Like

With this code :

awk '{printf("%s,%12.4f,%12.4f\n",strftime("%c",$1),$2,$3) } '

I have space in at the third field, not a big problem. Ummm I think the space is correct. :slight_smile:

Fri 21 Mar 2014 05:30:00 AM WIT, 778785.6976, 235032.1928
Fri 21 Mar 2014 06:00:00 AM WIT, 850720.1745, 291613.5261
Fri 21 Mar 2014 06:30:00 AM WIT,1136718.0249, 370101.2030
Fri 21 Mar 2014 07:00:00 AM WIT,1246803.9930, 443087.2727
Fri 21 Mar 2014 07:30:00 AM WIT,1456401.2317, 460454.3023
Fri 21 Mar 2014 08:00:00 AM WIT,1378386.3119, 500824.3566

Could you explain the logic behind the code anbu23 ?

and with this

awk -v OFMT='%.4f' '{print strftime("%c",$1)","$2+0","$3+0}'

I have result like these :

Fri 21 Mar 2014 05:30:00 AM WIT,778786,235032
Fri 21 Mar 2014 06:00:00 AM WIT,850720,291614
Fri 21 Mar 2014 06:30:00 AM WIT,1.13672e+06,370101
Fri 21 Mar 2014 07:00:00 AM WIT,1.2468e+06,443087
Fri 21 Mar 2014 07:30:00 AM WIT,1.4564e+06,460454
Fri 21 Mar 2014 08:00:00 AM WIT,1.37839e+06,500824

Appreciate your solution Don. I'm using Ubuntu using /usr/bin/gawk fyi.

Thanks all.

1 Like