3.3 Selecting by Position

As was explained in the previous sections, recsel outputs all the records of some record set. The records are echoed in the same order they are written in the recfile. However, often it is desirable to select a subset of the records, determined by the position they occupy in their record set.

The -n command line option to recsel supports doing this in a natural way. This is how we would retrieve the first contact listed in a contacts database using recsel:

$ recsel -n 0 contacts.rec
Name: Granny
Phone: +12 23456677

Note that the index is zero-based. If we want to retrieve more records we can specify several indexes to -n separated by commas. If a given index is too big, it is simply ignored:

$ recsel -n 0,1,999 contacts.rec
Name: Granny
Phone: +12 23456677

Name: Doctor
Phone: +12 58999222

With -n, the order in which the records are echoed does not depend on the order of the indexes passed to -n. For example, the output of recsel -n 0,1 will be identical to the output of recsel -n 1,0.

Ranges of indexes can also be used to select a subset of the records. For example, the following call would also select the first three contacts of the database:

$ recsel -n 0-2 contacts.rec
Name: Granny
Phone: +12 23456677

Name: Doctor
Phone: +12 58999222

Name: Dad
Phone: +12 88229900

It is possible to mix single indexes and index ranges in the same call. For example, recsel -n 0,5-6 would select the first, sixth and seventh records.