2 # Input: Each input line has one of the following two forms:
4 # string " %key " sort.key (tab) numlist
5 # Output: Each output line has the form:
6 # sort.key (tab) string (tab) numlist
8 BEGIN { FS = OFS = "\t" }
10 / %key / { # use sort.key if it is provided
11 i = index($1, " %key ")
12 print substr($1, i+6), substr($1, 1, i-1), $2
16 { # generate sort.key (in $2, by modifying string) if it is not provided
21 # Remove some troff commands
22 gsub(/\\f\(..|\\f.|\\s[+-][0-9]|\\s[0-9][0-9]?/, "", $2)
24 # underscore -> 0, so "foo_gorp" sorts before "food"
27 # quote character is %, space character is ~
29 if ($2 ~ /%/) { # hide quoted literals in Q
31 gsub(/%%/, "QQ0QQ", $2)
32 gsub(/%\[/, "QQ1QQ", $2)
33 gsub(/%\]/, "QQ2QQ", $2)
34 gsub(/%\{/, "QQ3QQ", $2)
35 gsub(/%\}/, "QQ4QQ", $2)
36 gsub(/%~/, "QQ5QQ", $2)
38 gsub(/%e/, "\\", $2) # implement troff escape
39 gsub(/~/, " ", $2) # remove tildes
40 gsub(/[%\[\]\{\}]/, "", $2) # remove % and font-changing []{}
41 if (quoted) { # restore literals but without escape charcter
42 gsub(/QQ0QQ/, "%", $2)
43 gsub(/QQ1QQ/, "[", $2)
44 gsub(/QQ2QQ/, "]", $2)
45 gsub(/QQ3QQ/, "{", $2)
46 gsub(/QQ4QQ/, "}", $2)
47 gsub(/QQ5QQ/, "~", $2)
49 if ($2 ~ /^[^a-zA-Z]+$/) # purely nonalphabetic lines go first
51 else if ($2 ~ /^[0-9]/) # lines with eading digits come next
53 # otherwise whatever final.sort does