Basic Commands
Lesson 6: Record Selection (xtsel) Part I

The most conspicuous component for data processing is the ability to query records, which is accomplished by xtsel command in MUSASHI. This command allows users to prescribe a condition on what to select and how selection can be done. xtsel commands supports the use of operator at which it is used to compare strings and numbers. The following tables shows the list of operators supports by xtsel.

operator meaning
== numerical values on both sides are equal
< the numerical value on the left-hand side is smaller than that on the right-hand side
<= the numerical value on the left-hand side is not larger than that on the right-hand side
> numerical value on the left-hand side is larger than that on the right-hand side
>= numerical value on the left-hand side is not smaller than that on the right-hand side
<> numerical value on the left-hand side is not equal to that on  the right-hand side
-eq character strings on both sides are equal
-gt character string on the left-hand side is greater than that on the right-hand side
-ge character string on the left-hand side is not smaller than that on the right-hand side
-lt character string on the left-hand side is smaller than that on the right-hand side
-le character string on the left-hand side is not greater than that on the right-hand side
-ne character strings on both sides are equal
|| disjunction of logical expressions on both sides
&& conjunction of logical expression on both sides
=~ pattern matching by regular expression

Summary of options and usage: xtsel


Using xtsel

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 "xtsel.sh"

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

Method 1: The use of conditional operator on numeric values

Goal: Select the quantity and amount for transactions occured on October 20th, 2002.

Methodology: Select "Date","Quantity" and "Amount" by xtcut, the result is piped to xtsel. xtsel will then select the records whose date matches "20021020". 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 "xtsel.xt".

Specify the parameters as follows:

Condition - -c '$Date==20021020'
Note: -c defines the condition using a comparsion operator at which if date equals to "20021020", it returns true and the record will be selected.
Important: Be sure that the argument is specified in single quotes!!!! The attribute to be compared is preceeded by "$" as it will be treated as a dynamic variable for the date attribute for each record to compare against the exact condition "20021020".

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

#---- title
title="Tutorial"

#---- comment
comment="xtsel"

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

#---------------------------------------------------------------
  # command
#---------------------------------------------------------------
xtcut -f Date,Quantity,Amount -i $inPath/dat.xt |
xtsel -c '$Date==20021020' |
xtheader -l "$title" -c "$comment" -o xtsel.xt #===============================================================

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

<?xml version="1.0" encoding="euc-jp"?>
<xmltbl version="1.00">
<header>
<title>
Tutorial
</title>
<comment>
xtagg
</comment>
<field no="1">
<name>Date</name>
</field>
<field no="2">
<name>Quantity</name>
</field>
<field no="3">
<name>Amount</name>
</field>
</header>
<body><![CDATA[
20021020 1 431
20021020 1 348
20021020 1 509
20021020 1 370
20021020 1 337
20021020 1 272

Method 2: The use of conditional operator for character strings

When defining a condition for comparsion with character strings, the arguments will be declared differently. "-eq" will be used in place of "==" operator, and the string "20021020" has to be enclosed in quotes. The script should like as follows:

xtcut -f Date,Quantity,Amount -i $inPath/dat.xt |
xtsel -c '$Date -eq "20021020"' |
xtheader -l "$title" -c "$comment" -o xtsel.xt

Method 3: The use of conjunction operator

Where exist there are two conditions at which you would like to build in when selecting records, the conjunction operator "&&" comes in handy. If you want to select transactions with quantity larger than 5 and amount larger than or equal to 1000, you can specify the argument as '$qunatity>5 && $amount>=1000'.

xtcut -f Date,Quantity,Amount -i $inPath/dat.xt |
xtsel -c '$Quantity>5 && $Amount>=1000' |
xtheader -l "$title" -c "$comment" -o xtsel.xt
Note that the priority of the operators differs. ">" and ">=" are given a higher priority than "&&". ">" and ">=" have the same priority, so whichever comes first will be executed. In addition, priority can be defined using parenthesis. As an example, if we are to select transactions occured after 20021015 where the quanityt is larger than 5 or amount is larger than or equal to 1000, we can define the argument as follows:
'$Date>20020115 && ($Quantity>5 || $Amount>=1000)'
The condition "$Quantity>5 || $Amount>=1000" is placed in parenthese for they are treated as one condition to be compared with "$Date>20020115".

Method 4: The use of arithmetic operators

In addition to comparsion operators, various arithmetic operators used in xtcal can also be used.

Given that "Amount = Unit Price X Quantity", let's select the records whose unit price is less than or equal to 100 yen. The condition should be set up as follows:

xtcut -f Date,Quantity,Amount -i $inPath/dat.xt |
xtsel -c '($amount/$quantity) <=100' |
xtheader -l "$title" -c "$comment" -o xtsel.xt

One Point: Difference between comparisons based on character string and numerical value
As shown in the example above, the result of the comparison with numerical value is the same as that for character string, beware it's not always the same case. By numeric comparsion, "100" is greater than "20. When comparsion is done on character strings, "20" is larger because the comparsion is based on lexicographical ordering, i.e. the two strings will assumed to be of the same length and the first characters of the two strings will be compared.


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)
Total quantity and amount per day for transactions after "20021120" with amount greater than 1000. xtsel1.sh xtsel1.xt xtsel1.html
Create a report to display the total quantity and total amount for each day at which transactions took place after 5pm (Time: 170000) and the unit price is less than or equal to 100. xtsel2.sh xtsel2.xt xtsel2.html
Create a report to display the total quantity and amount for each day for manufacturer "0001" "0001" xtsel3.sh xtsel3.xt xtsel3.html
Create a report to display the total quantity and amount amount per day for manufacturer "0001" or "0002" xtsel4.sh xtsel4.xt xtsel4.html
< href="../index.html">Home  |  Next> Lesson 7: Record Selection II