Skip to content

Instantly share code, notes, and snippets.

@qmacro
Last active September 29, 2022 00:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qmacro/c84f5a17dc4740dc2defa6a913cd3c2c to your computer and use it in GitHub Desktop.
Save qmacro/c84f5a17dc4740dc2defa6a913cd3c2c to your computer and use it in GitHub Desktop.
# Output fields in a nicely arranged columnar format.
# Takes two optional variables:
# COLS: comma-separated list of column nrs (fields) to output (default: all)
# GAP: gap required between each column (default: 1)
# Take care of the GAP default
BEGIN {
if (GAP == "") GAP=1
}
# We need to take care of the COLS default right at the start, when
# we see the first record, so that we can set it to 'all', i.e. to
# however many fields there are in this first record.
NR == 1 {
# Get the value for COLS (should be a list like 5,6,7), falling
# back to all of the fields (for which we need to count NF).
if (COLS) {
split(COLS, fieldlist, ",")
} else {
for (i = 1; i <= NF; i++) {
fieldlist[i]=i
}
}
}
# Store each record for an eventual second pass through in END - in
# this first pass we need to work out the longest field in each
# column.
{
records[NR]=$0
for (i = 1; i <= NF; i++) {
if (length($i) > fieldlengths[i]) fieldlengths[i] = length($i)
}
}
# Second pass through the records, but by this stage we know how much
# padding to use for each column.
END {
for (record in records) {
split(records[record], fields, FS)
for (field in fieldlist) {
f = fieldlist[field]
printf "%*-s", fieldlengths[f] + GAP, fields[f]
}
printf "\n"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment