Blob


1 awk ' # range.collapse
2 # Input: lines of form: string (tab) ["b"|"e"|"a"] (tab) number
3 # Output: lines of form: string (tab) num [(space) num]
4 # In sequence of lines with same value of string:
5 # b line and following e line are combined into single line:
6 # string (tab) num num
7 # a line disappears if between paired b and e
8 # a line otherwise becomes single line:
9 # string (tab) num
11 function error(s) {
12 print "range.collapse: " s " near pp " rlo "-" rhi | "cat 1>&2"
13 }
14 function printoldrange() {
15 if (range == 1) { error("no %end for " term); rhi = "XXX" }
16 if (NR > 1) {
17 if (rlo == rhi)
18 print term, rlo
19 else
20 print term, (rlo " " rhi)
21 }
22 rlo = rhi = $3 # bounds of current range
23 }
25 BEGIN { FS = OFS = "\t" }
26 $1 != term { printoldrange(); term = $1; range = 0 }
27 $2 == "e" { if (range == 1) { range = 0; rhi = $3 }
28 else { printoldrange(); error("no %begin for " term); rlo = "XXX" }
29 next
30 }
31 $3 <= rhi + 1 { rhi = $3}
32 $3 > rhi + 1 { if (range == 0) printoldrange() }
33 $2 == "b" { if (range == 1) error("multiple %begin for " term); range = 1 }
34 END { if (NR == 1) NR = 2; printoldrange() }
35 ' $*