# fieldLengths is specified on the command line with -v # "4,3" would mean 2 fields, length 4 and 3. # rightJustify also with -v. # "3,6" would right justify fields 3 and 6. { fieldNum=split(fieldLengths, fieldSpec, ",") for (i=1;i<=fieldNum;i++) { gsub(" ", "", $i) # build formatStr as %s, or %- for left just formatStr="%"; if (!match(rightJustify, "\\<"i"\\>")) formatStr=formatStr"-" formatStr=formatStr fieldSpec[i]"s" printf(formatStr, $i) if (i != fieldNum) printf(",") else printf("\n") } }