Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make nice columns from random delimited files; here's how to use it: http://shelr.tv/records/4f9f20d59660807992000059
#!/usr/bin/awk -f
#BEGIN{FS="\t";}
BEGIN{
if (DELIM == 0)
DELIM=" "
if (SPACING == 0)
SPACING = " "
}
{
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]
printf "%-*s%s", max[f]+SPACING, fields[f], DELIM
print fields[f] # the last field need not be padded
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment