awk question.

Hi Experts,
I have the following awk program which works fine except that the total amt field is not a 10 digit field after the additions and deletions.

for example, if :
parta=00000100
partb=00000100
totalamount=0000000900

After the awk program is run.
total amount=700

Is there a way in awk that the field can be converted to a 10 digit field so that total amount =0000000700

{
out_filel="shtemp/MAMMR"

parta=substr($0,263,8)
partb=substr($0,271,8)
totalamt=substr($0,299,10)
firstsection=substr($0,1,298)
lastsection=substr($0,310,176)
totalamt=(totalamt-(parta+partb))

##print firstsection""%10itotalamt""lastsection > out_filel
print firstsection""totalamt""lastsection > out_filel
}

I had tried that with the command below but is not working for some reason

print firstsection""%10itotalamt""lastsection > out_filel

Any help will be appreciated!

Have you looked at the printf function? It has several options to control printing leading zero, decimal places, etc...

Also, take a look at --

$ echo 13 | awk '{printf "%010.0f\n",$1}'
0000000013

Hi nua7 ,
>Is there a way in awk that the field can be converted to a 10 digit field so that total amount =0000000700

  • check this out:
echo "700" | awk '{printf "%010d\n",$0}'
0000000700
1 Like