Use awk to selectively zero left-pad certain fields only -
my file has following format:
1/2/2559,11:58:00,4/1/2559,09:36:04,10,55,my name
i want use awk (among other things) left-pad 0 6th column.
for e.g., output may be:
my name,1/2/2559/11:58:00,10,0055,4/1/2559,09:35:04
so in example above, both 5th , 6th columns number fields want left pad 1 of fields.
the printf examples got other posts far (say using printf ($7,$1,$2,$5,"%04d\n",$6,$3,$4)
seems scope in fields or terminates current row upon reaching field left-padded.
would appreciate pointers on this, thanks.
this doesn't match expected output posted don't see how input posted may you're looking for:
$ awk -f, '{printf "%s,%s,%s,%s,%04d,%s,%s\n",$7,$1,$2,$5,$6,$3,$4}' file name,1/2/2559,11:58:00,10,0055,4/1/2559,09:36:04
if not edit question explain mapping between sample input , expected output.
consider also:
$ awk 'begin{fs=ofs=","} {$6=sprintf("%04d",$6)} 1' file 1/2/2559,11:58:00,4/1/2559,09:36:04,10,0055,my name
Comments
Post a Comment