Next: , Up: Selection Expressions   [Contents][Index]


3.5.1 Selecting by predicate

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:

$ recsel -e "Age < 18" -P Name acquaintances.rec
Bart Simpson
Adrian Mole

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 people whose ages are less than 18. This is an example of a selection expression. In this case it is a simple test, but it can be as complex as needed.

Next, there is -P which tells recsel to print out the value of the 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 on every birthday).

# Date of Birth
%type: Dob date

Name: Alfred Nebel
Dob: 20 April 2010
Email: alf@example.com

Name: Bertram Worcester
Dob: 3 January 1966
Email: bert@example.com

Name: Charles Spencer
Dob: 4 July 1997
Email: charlie@example.com

Name: Dirk Hogart
Dob: 29 June 1945
Email: dirk@example.com

Name: Ernest Wright
Dob: 26 April 1978
Email: ernie@example.com

Now we can achieve a similar result as before, by looking up the names of all those people 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

The >> 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 -e argument, each introducing a selection expression, in which case the records which satisfy all expressions are selected. You can provide more than one field label to -P or -p in order to select additional fields to be displayed. For example, if you wanted to send an email to all children 14 to 18 years of age, and today’s date were 1st August 2012, 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: charlie@example.com

As you can see, there is only one such child in our record set.

Note that the example command shown above contains both double quotes " and single quotes '. The double quotes are interpreted by the shell (e.g. bash) and the single quotes are interpreted by recsel, defining a string. (And the backslash is interpreted by the shell, the usual continuation character so that this manual doesn’t have a too-long line.)


Next: , Up: Selection Expressions   [Contents][Index]