If you want to verify each of the conditions stated and print warnings when the conditions aren't met, try the following:
#!/bin/ksh
awk '/[^[:digit:]]/ {
h[++hc] = $0
next
}
{ o[hc,++oc[hc]] = $0
}
END { if(hc != 2) {
printf("Expected 2 heading lines; found %d: ", hc)
for(i = 1; i <= hc; i++)
printf("\"%s\"%s", h, i == hc ? "\n" : " ")
ec = 1
}
if(oc[1] != oc[2] || oc[1] < 5 || oc[1] > 22) {
printf("%s\n%s %d %s \"%s\" and %d %s \"%s\".\n",
"Expected 5 <= x <= 22 entries under both headings;",
"found", oc[1], "under heading", h[1], oc[2],
"under heading", h[2])
ec += 2
}
printf("%-3s%s\n", h[1], h[2])
c = oc[1] > oc[2] ? oc[1] : oc[2]
last = -1
for(i = 1; i <= c; i++) {
if(i <= oc[1] && o[1, i] <= last) {
ec += ec % 8 < 4 ? 4 : 0
m = sprintf(" %d not > %d.",
o[1, i], last)
} else m = ""
if(i <= oc[1] && length(o[1, i]) != 2) {
ec += ec % 16 < 8 ? 8 : 0
m = sprintf("%s 1st field not 2 digits.", m)
}
if(i <= oc[2] && length(o[2, i]) != 7) {
ec += ec < 16 ? 16 : 0
m = sprintf("%s 2nd field not 7 digits.\n", m)
} else m = sprintf("%s\n", m)
printf("%s %s%s", o[1, i], o[2, i], m)
last = o[1, i]
}
exit ec
}' Input
Replace /bin/ksh
in #!/bin/ksh
with an absolute pathname of the Korn shell (or any shell that conforms to POSIX shell command language requirements) on your system.
If you're on a Solaris system, use /usr/xpg4/bin/awk
or nawk
instead of awk
.
The exit code will be 0 if all conditions are met, or 1 through 31 depending on which set of conditions fail.