10.1 Grouping Records

Consider a recfile containing a list of items in a shop inventory. For each item it is stored its type, its category, its price, the date of the last selling operation of an item of that type, and the amount of items currently available in stock. A sample of such a database could be:

Type: EC Car
Category: Toy
Price: 12.2
LastSell: 20-April-2012
Available: 623

Type: Terria
Category: Food
Price: 0.60
LastSell: 22-April-2012
Available: 8239

Type: Typex
Category: Office
Price: 1.20
LastSell: 22-April-2012
Available: 10878

Type: Notebook
Category: Office
Price: 1.00
LastSell: 21-April-2012
Available: 77455

Type: Sexy Puzzle
Category: Toy
Price: 6.20
LastSell: 6.20
Available: 12

Now imagine we are interested in grouping the contents of the Items record set in groups of items of the same category. We can do it using the -G command line argument for recsel. This argument accepts a list of fields separated by commas. The argument can be read as “group by”.

In this case we want to group by Category, so we would do:

$ recsel -G Category
Type: Terria
Category: Food
Price: 0.60
LastSell: 22-April-2012
Available: 8239

Type: Typex
Category: Office
Price: 1.20
LastSell: 22-April-2012
Available: 10878
Type: Notebook
Price: 1.00
LastSell: 21-April-2012
Available: 77455

Type: EC Car
Category: Toy
Price: 12.2
LastSell: 20-April-2012
Available: 623
Type: Sexy Puzzle
Price: 6.20
LastSell: 6.20
Available: 12

We can see that the output is three records, corresponding to the three different categories of items present in the database. However, we are only interested in the types of products in each category, so we can remove unwanted information using -p:

$ recsel -G Category -p Category,Type items.rec
Category: Food
Type: Terria

Category: Office
Type: Typex
Type: Notebook

Category: Toy
Type: EC Car
Type: Sexy Puzzle

It is also possible to group by several fields. We could group by both Category and LastSell:

$ recsel -G Category,LastSell -p Category,LastSell,Type items.rec
Category: Food
LastSell: 22-April-2012
Type: Terria

Category: Office
LastSell: 21-April-2012
Type: Notebook

Category: Office
LastSell: 22-April-2012
Type: Typex

Category: Toy
LastSell: 20-April-2012
Type: EC Car

Category: Toy
LastSell: 6.20
Type: Sexy Puzzle