Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Align-columns/AWK/align-columns.awk
Normal file
41
Task/Align-columns/AWK/align-columns.awk
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# syntax: GAWK -f ALIGN_COLUMNS.AWK ALIGN_COLUMNS.TXT
|
||||
BEGIN {
|
||||
colsep = " " # separator between columns
|
||||
report("raw data")
|
||||
}
|
||||
{ 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 {
|
||||
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) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue