Help with File Processing (AWK)

Input File:

1234, 2345,abc
1,24141,gw
222,rff,sds
2232145,sdsd,121

Output file to be generated:

000001234,2345,abc
000000001,24141,gw
000000222,rff,sds
002232145,sdsd,121

i.e; the first column is padded to get 9 digits.

I tried with following:

 awk -F"," 'BEGIN{OFS=",";} {printf ("%09d\n",$1 $2 $3)}' testfile

But I am not getting the desired output. Somewhere I am doing mistake in printf.

Try this:

awk -F"," '{printf("%09s,%s,%s\n", $1,$2,$3)}' file

000001234, 2345,abc
000000001,24141,gw
000000222,rff,sds
002232145,sdsd,121

Hope this helps.

Alternatively:

awk -F, '$1=sprintf("%09d",$1)' OFS=, infile

The post http://www.unix.com/shell-programming-scripting/176657-help-file-processing-awk.html has been closed by forum staff.

IMO, we need to change the FS in awk to include space otherwise we will not be able to remove the leading space in field 2

awk 'BEGIN{FS="[, ]+"}{printf("%09d,%s,%s\n",$1,$2, $3)}'