Thursday, July 8, 2010

columnate

#!/usr/bin/awk -f

BEGIN{FS="\t";}

{
line[NR] = $0 # saves the line
for (f=1; f<=NF; f++) {
len = length($f)
if (len>max[f])
max[f] = len # an array of maximum field widths
}
}

END {
for(nr=1; nr<=NR; nr++) {
nf = split(line[nr], fields)
for (f=1; f printf "%-*s", max[f]+2, fields[f]
print fields[f] # the last field need not be padded
}
}

No comments:

Post a Comment