Consider the example recfile acquaintances.rec introduced earlier. It contains names of people along with their respective ages. Suppose we want to get a list of the names of all the children. It would not be easy to do this using grep. Neither would it, for any reasonably large recfile, be feasible to search manually for the children. Fortunately the recsel command provides an easy way to do such a lookup:
$ recfix acquaintances.rec $ recsel -e "Age < 18" -P Name acquaintances.rec Bart Simpson Adrian Mole
Before the recsel command there is a call to
recfix, just in case acquaintances.rec contains
Let us look at each of the arguments to recsel in turn:
Firstly we have
-e which tells recsel to lookup records
matching the expression
Age < 18 — in other words all those persons
whose ages are less than 18.
This is an example of a selection expression.
In this case it is a very simple one, but it can be as complex as needed.
Next there is
-P which tells recsel to print out the value of
Name field — because we want just the name, not the entire record.
The final argument is the name of the file from whence the records are
to come: acquaintances.rec.
Rather than explicitly storing ages in the recfile, a more realistic example might have the date of birth instead (otherwise it would be necessary to update the people's ages in the recfile every year).
# Date of Birth %type: Dob date Name: Alfred Nebel Dob: 20 April 2010 Email: firstname.lastname@example.org Name: Bertram Worcester Dob: 3 January 1966 Email: email@example.com Name: Charles Spencer Dob: 4 July 1997 Email: firstname.lastname@example.org Name: Dirk Hogart Dob: 29 June 1945 Email: email@example.com Name: Ernest Wright Dob: 26 April 1978 Email: firstname.lastname@example.org
Now we can achieve a similar result as before, by looking up the names of all those persons who were born after a particular date:
$ recfix acquaintances.rec $ recsel -e "Dob >> '31 July 1994'" -p Name acquaintances.rec Name: Alfred Nebel Name: Charles Spencer
>> operator means “later than”, and is used
here to select a date of birth after 31st July 1994.
Note also that this example uses a lower case
-p whereas the preceding example
used the upper case
-P. The difference is, that
-p prints the field name
and field value, whereas
-P prints just the value.
Recsel accepts more than one
each introducing a selection expression,
in which case records which satisfy all expressions are selected.
You can provide more than one field label to
-p in order to select
additional fields to be displayed.
For example, if you wanted to send an email to all children 14 years of age or older,
and today's date were 1st August 1994, then you could use the following command to get
the name and email address of all such children:
$ recfix acquaintances.rec $ recsel -e "Dob >> '31 July 1994' && Dob << '01 August 1998'" -p Name,Email acquaintances.rec Name: Charles Spencer Email: email@example.com
As you can see, there is only one such child in the record set.
Note that the example command shown above contains both double quotes
The double quotes are interpreted by the shell (e.g. bash) and
the single quotes are part of the string which forms the two dates being compared.