grpsort.icn: Program to sort groups of lines

June 10, 1988; Thomas R. Hicks
This file is in the public domain.
   This program sorts input containing ``records'' defined to be
groups of consecutive lines. Output is written to standard out-
put.  Each input record is separated by one or more repetitions
of a demarcation line (a line beginning with the separator
string).  The first line of each record is used as the key.

   If no separator string is specified on the command line, the
default is the empty string. Because all input lines are trimmed
of whitespace (blanks and tabs), empty lines are default demarca-
tion lines. The separator string specified can be an initial sub-
string of the string used to demarcate lines, in which case the
resulting partition of the input file may be different from a
partition created using the entire demarcation string.

   The -o option sorts the input file but does not produce the
sorted records.  Instead it lists the keys (in sorted order) and
line numbers defining the extent of the record associated with
each key.

   The use of grpsort is illustrated by the following examples.
The command

        grpsort "catscats" <x >y

sorts the file x, whose records are separated by lines containing
the string "catscats", into the file y placing a single line of
"catscats" between each output record. Similarly, the command

        grpsort "cats" <x >y

sorts the file x as before but assumes that any line beginning
with the string "cats" delimits a new record. This may or may not
divide the lines of the input file into a number of records dif-
ferent from the previous example.  In any case, the output
records will be separated by a single line of "cats".  Another
example is

        grpsort -o <bibliography >bibkeys

which sorts the file bibliography and produces a sorted list of
the keys and the extents of the associated records in bibkeys.
Each output key line is of the form:

        [s-e] key

where

        s     is the line number of the key line
        e     is the line number of the last line
        key   is the actual key of the record

Source code | Program Library Page | Icon Home Page