insert leading zeroes based on the character count

Hi,

I need add leading zeroes to a field in a file based on the character count. The field can be of 1 character to 6 character length. I need to make the field 14bytes.

eg:
8351,20,1
8351,234,6
8351,2,0
8351,1234,2
8351,123456,1
8351,12345,2

This should become.

8351,00000000000020,1
8351,00000000000234,6
8351,00000000000002,0
8351,00000000001234,2
8351,00000000123456,1
8351,00000000012345,2

Any help will be much appreciated.

Try this:

awk -F, '{$2=sprintf("%014d",$2)}1' OFS="," file
  awk 'BEGIN { FS = ","; OFS = "," }; {$2=sprintf("%014d",$2)}1' filename 

damn, you beat me...

Thanks guys. It worked :slight_smile: