[Home]
HomePage RecentChanges Frequently Asked Questions

Columnate

This script columnates the input file, so that columns line up like in the GNU column(1) command. Its output is like that of column -t. First, awk reads the whole file, keeps track of the maximum width of each field, and saves all the lines/records. At the END, the lines are printed in columnated format. If your terminal is not too narrow, you'll get a handsome display of the file.

For an example of the output, you can try columnate -F : /etc/passwd.

{
 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<nf; f++)
 printf "%-*s", max[f]+2, fields[f]
 print fields[f] # the last field need not be padded
 }
}

Edit this page View other revisions Administration
Last edited 2009年02月28日 21:32 UTC by h-67-101-152-180.nycmny83.dynamic.covad.net (diff)

Powered by sdf Creative Commons License <pierre.gaston+awk@gmail.com>

Awk Wiki by #awk is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

AltStyle によって変換されたページ (->オリジナル) /