Skip to content

Instantly share code, notes, and snippets.

@gregmark
Last active August 7, 2022 00:18
Show Gist options
  • Save gregmark/05ac2a108f8151539df9f538330a17db to your computer and use it in GitHub Desktop.
Save gregmark/05ac2a108f8151539df9f538330a17db to your computer and use it in GitHub Desktop.
p2columns.awk
# p2columns.awk
# -------------
# * aligns two-field output into two even columns
# * column width will equal the number of characters in the longest first-field
# string plus the value of the variable p
# * by default, p (padding) == 1
# * change padding with -v p=N, e.g. to increase padding to 3 spaces:
#
# awk -v p=3 -f awk-script input-file
#
BEGIN {
if (! p) p = 1
n = 0;
};
{
x = length($1);
if (x > n) n = x;
a[NR] = $0;
};
END {
n += p;
for ( i = 1; i <= NR; i++ ) {
split(a[i], z);
printf "%-"n"s%s\n", z[1], z[2];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment