Prepend 0 to a field in a record

Hi All,

I have a file like below. In the field 9 I am having 14,014,3,001/009 on the records. I want to convert the field to a three digit value. For example 14 should 014 , 3 should 003

11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;14;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;1;3;29=0000;1.25
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;001/009;3;29=0000;1.25

Expected

11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;014;003;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;1;003;29=0000;1.25
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;001/009;3;29=0000;1.25

Any attempts / ideas / thoughts from your side?

awk '-F;' '{print $9}' file
11095
11095
11095
11095

With the previous thread I tried to do like and getting all zeros

 awk '{ printf "%03d",$9}'  myfile

---------- Post updated at 10:51 PM ---------- Previous update was at 10:48 PM ----------

Sorry it is 12 the column

awk '-F;' '{print $12}' file

14
014
3
001/009

Try:

awk '{$12=sprintf("%03d",$12)}1' FS=\; OFS=\; file

or

awk -F\; -v OFS=\; '{$12=sprintf("%03d",$12)}1' file

or

awk 'BEGIN{FS=OFS=";"} {$12=sprintf("%03d",$12)}1' file
1 Like

In order to retain the special structure of the fourth line's $12, try

awk -F\; '$12 == $12+0 {$12 = sprintf ("%03d", $12)} 1' OFS=";" file
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;001;3;29=0000;1.25
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;001/009;3;29=0000;1.25
2 Likes

I'm pretty sure in your post#1's sample, the third line's $12 is 1 .