2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,34 +1,41 @@
# syntax: GAWK -f ALIGN_COLUMNS.AWK ALIGN_COLUMNS.TXT
BEGIN {
FS="$"
lcounter = 1
maxfield = 0
# justification; pick one
#justify = "left"
justify = "center"
#justify = "right"
colsep = " " # separator between columns
report("raw data")
}
{
if ( NF > maxfield ) maxfield = NF;
for(i=1; i <= NF; i++) {
line[lcounter,i] = $i
if ( longest[i] == "" ) longest[i] = 0;
if ( length($i) > longest[i] ) longest[i] = length($i);
}
lcounter++
{ printf("%s\n",$0)
arr[NR] = $0
n = split($0,tmp_arr,"$")
for (j=1; j<=n; j++) {
width = max(width,length(tmp_arr[j]))
}
}
END {
just = (justify == "left") ? "-" : ""
for(i=1; i <= NR; i++) {
for(j=1; j <= maxfield; j++) {
if ( justify != "center" ) {
template = "%" just longest[j] "s "
} else {
v = int((longest[j] - length(line[i,j]))/2)
rt = "%" v+1 "s%%-%ds"
template = sprintf(rt, "", longest[j] - v)
}
printf(template, line[i,j])
}
print ""
}
report("left justified")
report("right justified")
report("center justified")
exit(0)
}
function report(text, diff,i,j,l,n,r,tmp_arr) {
printf("\nreport: %s\n",text)
for (i=1; i<=NR; i++) {
n = split(arr[i],tmp_arr,"$")
if (tmp_arr[n] == "") { n-- }
for (j=1; j<=n; j++) {
if (text ~ /^[Ll]/) { # left
printf("%-*s%s",width,tmp_arr[j],colsep)
}
else if (text ~ /^[Rr]/) { # right
printf("%*s%s",width,tmp_arr[j],colsep)
}
else if (text ~ /^[Cc]/) { # center
diff = width - length(tmp_arr[j])
l = r = int(diff / 2)
if (diff != l + r) { r++ }
printf("%*s%s%*s%s",l,"",tmp_arr[j],r,"",colsep)
}
}
printf("\n")
}
}
function max(x,y) { return((x > y) ? x : y) }