Created
April 30, 2012 23:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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