# Pad zeroes first field in a Delimited file

**URL:** <https://community.unix.com/t/pad-zeroes-first-field-in-a-delimited-file/302529>\
**Category:** Shell Programming and Scripting\
**Created:** [January 10, 2012, 7:14pm UTC](https://community.unix.com/t/pad-zeroes-first-field-in-a-delimited-file/302529 "2012-01-10T19:14:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnhips](https://community.unix.com/letter_avatar/johnhips/32/5_5575768a8748004e209b776fc1b2916d.png) [@johnhips](https://community.unix.com/u/johnhips)\
**Post date:** [January 10, 2012, 7:14pm UTC](https://community.unix.com/t/pad-zeroes-first-field-in-a-delimited-file/302529/1 "2012-01-10T19:14:03Z")

</div>

Need help. I tried using an awk command to pad zeroes. Unfortunately, the "|" pipe delimited character is gone when I tried to write the records to another file.

```nohighlight
awk -F \| ' {$1=sprintf("%06s", $1); print $0}' $CUSTFINAL2 > $CUSTFINAL3

BEFORE
"KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT ID"|"PREM ID"|"PHONE"|"NAME"|"Name"|"MAIL ADDR1"|"MAIL ADDR2"|"MAIL CITY"|"MAIL STATE"|"MAIL 
1|" "|" "|"3197691605"|" "|"3197691657"|" "|"OCCUPANT                                                        
2|" "|" "|"3246309010"|" "|"3246309029"|" "|"OCCUPANT                               
3|" "|" "|"3350909705"|" "|"3311776290"|" "|"OCCUPANT                                                        
3|" "|" "|"3311776205"|" "|"3311776290"|" "|"OCCUPANT                                                        

AFTER
"KEYRECORD" "SA ID" "PER ID" "SP ID" "ACCT ID" "PREM ID" "PHONE" "NAME" "Name" "MAIL ADDR1" "MAIL ADDR2" "MAIL CITY" "MAIL STATE" "MAIL 
000001 " " " " "3197691605" " " "3197691657" " " "OCCUPANT                                                        
000002 " " " " "3246309010" " " "3246309029" " " "OCCUPANT                                                        
000003 " " " " "3350909705" " " "3311776290" " " "OCCUPANT                                                        
000003 " " " " "3311776205" " " "3311776290" " " "OCCUPANT                                                        
000004 " " " " "3329099805" " " "3329099858" " " "OCCUPANT                                                        

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [January 10, 2012, 7:24pm UTC](https://community.unix.com/t/pad-zeroes-first-field-in-a-delimited-file/302529/2 "2012-01-10T19:24:10Z")

</div>

Set OFS:

```nohighlight
awk -F \| ' {$1=sprintf("%06d", $1); print $0}' OFS=\| $CUSTFINAL2 > $CUSTFINAL3

```

---

<div class="post-metadata">

**Author:** ![johnhips](https://community.unix.com/letter_avatar/johnhips/32/5_5575768a8748004e209b776fc1b2916d.png) [@johnhips](https://community.unix.com/u/johnhips)\
**Post date:** [January 10, 2012, 7:56pm UTC](https://community.unix.com/t/pad-zeroes-first-field-in-a-delimited-file/302529/3 "2012-01-10T19:56:05Z")

</div>

So much thanks Chubler\_XL !!!!

---------- Post updated at 07:56 PM ---------- Previous update was at 07:29 PM ----------

Can I use printf in this awk to pad my variable 'n' counter???

```nohighlight
	awk -F \| '
	    NR == 1 { print "\"KEYRECORD\"|" $0; next; }
	    {
       	 if( $4 == "\" \"" && last5 != $5) {
       	     n++;
	        } 

	        print n "|" $0;
	        last = $4;
	        last5 = $5;

	    }
	' $CUSTFINAL > $CUSTFINAL2

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [January 11, 2012, 4:43pm UTC](https://community.unix.com/t/pad-zeroes-first-field-in-a-delimited-file/302529/4 "2012-01-11T16:43:47Z")

</div>

Yes just use:

```nohighlight
printf "%06d|%s\n", n, $0

```
