Basic Commands
Lesson 7: Record Selection (xtselstr) Part II

Another command that is capable for record selection is xtselstr command. It allows more detailed definition of what to extract over xtsel and is more efficient esp. when defining an argument to extract data matching multiple values on one attribute.

Summary of options and usage: xtselstr


Using xtselstr

Goal: Select the transactions occurred on January 20, 22, or 24 in 2002.

Step 1: Editing your script

Start FD in your working directory (/home/public/lessons/basic/) Repulciate the script "xtcut.sh" used in the previous lesson by selecting xtcut.sh and press "c+CTRL", you will then be prompted for a new name, name the new script as "xtselstr.sh"

copy the file xtcut.sh as a new name on the current directory
new name : xtselstr.sh
cp /home/public/lesson/basic/xtcut.sh /home/public/lesson/basic/xtselstr.sh

Methodology: Select "Date","Quantity" and "Amount" by xtcut, the result is piped to xtselstr, which selects the records whose date matches "20020120", "20020122", and "20020124". The output will be written as an input to xtheader, define a new title and comment as need, update the -o parameter and write the result to the file "xtselstr.xt".

Step 2: Defining Attributes and Options

Specify the parameters as follows:

Field - -f Date
Note: The argument of "-f" specifies the field where the conditional selection will be performed on. Multiple fields arguments may also be defined alternatively.

Value - -v 20020120,20020122,20020124
Note: -v defines the substring to be extracted, which are " 20020120,20020122,20020124". More than one character strings are delimited by comma without spaces in between.

Your script will look like:
#/bin/bash
#===============================================================
# MUSASHI bash script
#===============================================================

#---- title
title="Tutorial"

#---- comment
comment="xtselstr"

#---- variables
inPath="/home/public/tutorial"

#---------------------------------------------------------------
  # command
#---------------------------------------------------------------
xtcut -f Date,Quantity,Amount -i $inPath/dat.xt |
xtselstr -f Date -v 20020120,20020122,20020124 |
xtheader -l "$title" -c "$comment" -o xtselstr.xt #===============================================================

Step 3: Running the script

When you are done, save and execute the script. The result should look as follows:

20020120 1 343
20020120 1 309
20020120 1 483
20020120 1 422
20020120 4 1204
20020122 1 565

A hash list is used to store a list of character strings, therefore , the ordering of character strings do not affect the efficiency of the xtselstr command, neither does increasing the length of string.


One Point: Select command with various functions
Besides the above scenarios introduced in the tutorial, the commands xtsel and xtselstr has other various ways to describe queries. Specifically, declaring the key value is the most important component as it allows us to treat multiple records with some key attribute as one selection unit, where the conditions for selection will be applied. This will be explained in Session 2 of t this tutorial .


Exercises

Let's apply xtagg on the following reports. Check your results with the scripts and output files given below.

Report Description
Script name Output file (xt) Output file (html)
Display the total quantity and amount for manufacturer "0001","0002", and "0004". xtselstr1.sh xtselstr1.xt xtselstr1.html
Create a report to display the total quantity and total amount for 4-digit classification code which matches "1101", "1111", "1116", "1401", "1403", and "1406". xtselstr2.sh xtselstr2.xt xtselstr2.html
Create a report to display the total quantity and amount for transcation amount matching "99", "199" or "299". xtselstr3.sh xtselstr3.xt xtselstr3.html

Home  |  Next> Lesson 8: Delete Duplicate Keys