Columnate

Last edit

Summary: 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, . . .

Changed:

< 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.

to

> 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.

Changed:

< {{{awk

to

> {{{ awk


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
    }
}