Feb 7, 2001 #1 NPI Programmer Feb 7, 2001 4 US How can I get awk to print out the leading zeros when padding a field width? The command: awk ' BEGIN{ v_nmbr=1 printf( "%3d\n", v_nmbr ) } ' outputs 1. I would like to get 001. We hope that this helps. Regards, NPI
How can I get awk to print out the leading zeros when padding a field width? The command: awk ' BEGIN{ v_nmbr=1 printf( "%3d\n", v_nmbr ) } ' outputs 1. I would like to get 001. We hope that this helps. Regards, NPI
Feb 7, 2001 Thread starter #2 NPI Programmer Feb 7, 2001 4 US I auppose if might be poor forum to answer my own question but I just figured out the answer for integers. printf( "%.3d\n", v_nmbr ) will yield 001 I suppose that the question still remains on doing this with reals. i.e what if v_nmbr=1.1? Upvote 0 Downvote
I auppose if might be poor forum to answer my own question but I just figured out the answer for integers. printf( "%.3d\n", v_nmbr ) will yield 001 I suppose that the question still remains on doing this with reals. i.e what if v_nmbr=1.1?
Feb 14, 2001 #3 Krunek Programmer Feb 14, 2001 140 HR Hi! Try this picture: %3.3d Jesus loves you! Bye! Upvote 0 Downvote
Feb 14, 2001 Thread starter #4 NPI Programmer Feb 7, 2001 4 US Hey Everyone, Thanks for your help. It turns out that the answer is a combination of things I learned here and elsewhere. To print any number padded with zeros just place a 0 as the first character in the format specifier. For an integer: %03d For a real: %03.1f awk 'BEGIN{v_nmbr=1; printf("%03d\n", v_nmbr )}' 001 awk 'BEGIN{v_nmbr=1.1; printf("%05.1f\n", v_nmbr )}' 001.1 Upvote 0 Downvote
Hey Everyone, Thanks for your help. It turns out that the answer is a combination of things I learned here and elsewhere. To print any number padded with zeros just place a 0 as the first character in the format specifier. For an integer: %03d For a real: %03.1f awk 'BEGIN{v_nmbr=1; printf("%03d\n", v_nmbr )}' 001 awk 'BEGIN{v_nmbr=1.1; printf("%05.1f\n", v_nmbr )}' 001.1