Skip to content

Instantly share code, notes, and snippets.

@FlaviuSim
Created April 30, 2012 23:48
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 FlaviuSim/2563712 to your computer and use it in GitHub Desktop.
Save FlaviuSim/2563712 to your computer and use it in GitHub Desktop.
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