Add leading zeros to columns in a file

Hello Gurus,

Quick question. I have a file with the following records:

A~000000000000518000~SLP ~99991231~20090701~88.50~USD~CS~
A~000000000000518000~SLP ~99991231~20090701~102.00~USD~CS~
A~000000000000772000~SLP ~99991231~20100701~118.08~USD~CS~

I wold like to do the following:

  1. Add leading zeros before column 6, such that there are 5 places, and 2 decimal positions
  2. Add a space after the value of column 7 and another space after the value of column 8, such that the output file looks like the following:

A~000000000000518000~SLP ~99991231~20090701~00088.50~USD ~CS ~
A~000000000000518000~SLP ~99991231~20090701~00102.00~USD ~CS ~
A~000000000000772000~SLP ~99991231~20100701~00118.08~USD ~CS ~

Thanks in advance for your help!

Try:

awk -F"~" -vOFS="~" '{$6=sprintf ("%08.2f",$6);$7=$7" ";$8=$8" "}1' file