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