# Add leading zeros to columns in a file

**URL:** <https://community.unix.com/t/add-leading-zeros-to-columns-in-a-file/293066>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [August 8, 2011, 9:48am UTC](https://community.unix.com/t/add-leading-zeros-to-columns-in-a-file/293066 "2011-08-08T09:48:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chumsky](https://community.unix.com/letter_avatar/chumsky/32/5_5575768a8748004e209b776fc1b2916d.png) [@chumsky](https://community.unix.com/u/chumsky)\
**Post date:** [August 8, 2011, 9:48am UTC](https://community.unix.com/t/add-leading-zeros-to-columns-in-a-file/293066/1 "2011-08-08T09:48:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bartus11](https://community.unix.com/user_avatar/community.unix.com/bartus11/32/1418_2.png) [@bartus11](https://community.unix.com/u/bartus11)\
**Post date:** [August 8, 2011, 9:59am UTC](https://community.unix.com/t/add-leading-zeros-to-columns-in-a-file/293066/2 "2011-08-08T09:59:39Z")

</div>

Try:

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

```
