summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/eplain/util/idxuniq
blob: 6ee7d225f5c4fb8d8f4740946e9915f108a2a547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/awk -f

# (This file is public domain.)

# This script filters input lines (which are expected to be a list of
# `\indexentry' lines for MakeIndex) eliminating matching lines.  The comparison
# is done without regard to hyperlink label names (in the form `IDX*', where `*'
# stands for a non-negative integer).

# This has to be done to avoid terms which differ only in hyperlink label names
# embedded into them, because MakeIndex will treat these otherwise identical
# terms as distinct, listing more than once a page number for equivalent terms
# on the same page.

# `stripped' array is indexed by `\indexentry' lines stripped off the hyperlink
# label name.  For each term, we use its stripped version as a key into the
# `stripped' array to increment its element.  This records the fact that we have
# seen the term with such key.  Also, we add the (full) `\indexentry' line to
# the `terms' array, but only if we have not yet seen a term with such key.

# The idea was borrowed from Edition 3 of `GAWK: Effective AWK Programming: A
# User's Guide for GNU Awk', which contains the following credit:

#       histsort.awk --- compact a shell history file
#       Thanks to Byron Rakitzis for the general idea

{
  temp = $0
  sub (/{IDX[[:digit:]]+}/, "", temp)
  if (stripped[temp]++ == 0)
    terms[++count] = $0
}

END {
  for (i = 1; i <= count; i++)
    print terms[i]
}