xtcal
Section: User Commands (1)
Updated: 2002-10-26
Index
Return to Main Contents
NAME
xtcal - calculation
SYNOPSIS
xtcal -c mathematical expression -a name of new attribute
[-k key attribute] [-q] [-i INPUT] [-o OUTPUT] [-z] [-t] [-T TEMP DIRECTORY]
DESCRIPTION
This command performs various mathematical operations with respect to the key attribute -k serving as the base unit for calculation. When -k is omitted, all records will have their own key values.
Attributes used in mathematical functions are treatred as variables and is preceeded by the prefix '$': e.g. $Date, while operator symbols used in the functions should be enclosed by single quotes ' ' or they will be executed as shell scripts. For attributes with regular expression, it needs to be enclosed by parenthesis such as $(date*).
The following section describes operators and functions available in the xtcal command.
[arithmetic operator]
+, -, *, /: fundamental arithmetic operations -
ex) $amount+1
%: remainder from division -
ex) $quantity%4
<=, <, >=, >, <>, ==: comparing numerical values-
ex) $quantity>=$maximum quantity
-le,-lt,-ge,-gt,-ne,-eq: comparing character strings-
ex)$date -lt "19991212"
=~: comparsion with regular expression-
ex) $article name=~"^Baby.*"
||,&&: logical operators 'or' and 'and'-
ex) $quantity>10 && $amount<1000
[date and time functions]
age(date,birthday): age from date-
ex) age("20011210",$birthday) age($date,$birthday)
datefmt(date,format) : conversion of date format-
-
%Y:year, %M:month, %D:day
-
%d:the number of day from "date",
-
%w: week number when the week of "date" is the first week,
-
%W: the day number in a week,
-
%e: the day of the week in English,
-
%E:the day number in a week + the day of the week in English,
-
%j:the day of the week in Japanese,
-
%J:the day number in a week+the day of the week in Japanese
-
ex) datefmt($date,"%Y year%M month%D day");
datefmt($date,"%Y/%M/%D(%e)")
dayadd(date,number_of_days) the date after adding number_of_days-
ex) dayadd($date,10); dayadd("19991210",15);
day(date1,date2) : number of days between date1 and date2-
Note: The more recent date should preceed as date1.
ex) day($date,"20001230")
timefmt(time,format) : convert the time format-
%H:hour, %M:minute, %S:second
ex) timefmt($time,"%Hhour%Mminute %Ssecond")
timeadd(date,time,seconds) : time after adding seconds to date and time-
ex) timeadd($date,$time,10)
time(date1,time1,date2,time2) : the difference between date1:time1-date2:time2 in seconds-
ex) time($endDate,$endTime,$startDate,$startTime)
now(): return the current time-
today(): return the current date -
[mathematical functions]
sum(value1,value2, ...): sum of all values defined-
ex) sum($Quantity*), sum($Price1,$Price2,100);
sqrt(value): square root function-
power(value,exponent): value with power of "exponent"-
abs(value): absolute value function-
round(value,r):round off values to "r" degree of precision-
up(value,r): round up values at "r" degree of precision -
down(value,r): round down values at "r" degree of precision-
exp(value): value-th power of e-
ln(value): natural log of value-
log(value,b): log of value with base b-
log2(value): log of value with base 2-
log10(value): log of value with base 10-
pi(): pi function-
rand(min,d,seed): integral random number from the interval -
[min, min+1, ..., min+d-1]
nrand(m,sd,seed): random number derived from mean and standard deviation
[functions related to character string]
- cat(string1, string2, ...,token): concatenates string1, string2, ... by token
-
ex) cat($family name, $first name,"-"), cat("name is ",$name,"")
- length(string1) : the length of string1 in bytes
-
- fix(string1,length,pos,text delimiter): converts string1 to fixed length character string. If the string length exceeds 'length', the remaining part is discarded.
-
pos defines Right or Left alignment of string1.
pos=R justfies output string to the right.
- regexStart(string1,r): returns the starting position of string1 that matches regular expression 'r'
-
- regexEnd(string1,r): returns the last position of string 1 that matches regular expression 'r'
-
- regexLen(string1, r): returns the length of the string that matches regular expression 'r'
-
[logical function]
- if(condition1,string1,string2): if condition1 is true, execute string1, else execute string2
-
- not(logical value) : negation of logical value
-
[functions related to lines and attributes]
- line(): line number currently processed
-
- fldCnt(): number of attributes
-
- keyLine(): line number within the current key
-
- keyCnt(): number of keys currently processed
-
- keyNo(): count the sequential number of the key processed.
-
When keys are a1, a2, ..., aN in the sorted order and the current key is ak, it returns k.
- prvFldA(N, A): return the value of attribute A of the line whose absolute line number within the specified key is N.
-
- prvFldR(N, A): return the value of attribute A of the line whose relative line number with respect to the current line within the specified key is N. When the current line number is n, it returns the value at line N+n.
-
- prvRslA(N) : return the calculation result for the line whose absolute line number within the same key as the current one is N. N must be smaller than the current absolute line number within the key.
-
- prvRslR(N) : return the calculation result for the line whose relative line number within the same key as the current one is N. N must be a negative integer.
-
PARAMETERS
- -k key attribute(s)
-
key attribute as the unit of calculation. When omitted, all records will be based on one key unit.
- -c mathematical computation
-
method of computation by combining built-in operators and functions.
- -a new attribute
-
Specify the name of the new attribute created by this command. Renaming the current attribute with the expression $date:Date2 will not change the header.
- -q sequential processing
-
When this option is used with the -k parameter, the command processes the input data in original sequence of the lines, instead of sorting by the key attribute -k.
FILE OPTIONS
- -i input filename
-
if a suffix of the filename is '.gz', the command acts as a filter, extracting the compressed file for processing. The command will read the file as standard input when "-i" is not specified.
- -o output filename
-
if a suffix of the filename is '.gz', the command automatically returns the output data in zip archive. When "-o" is not specified, the result will sent to standard output.
- -z zip archive
-
compress the standard output to zip archive. When the option "-o" is not given and "-z" is specified, the output will be compressed as zip archive.
- -t plain text
-
xtagg treats the input and output data as plain text format.
USAGE
Input file - dat.xt:
<field no="1">
<name>CustomerID</name>
</field>
<field no="2">
<name>Date</name>
</field>
<field no="3">
<name>TotalQuantity</name>
</field>
<field no="4">
<name>TotalAmount</name>
</field>
</header>
<body><![CDATA[
A00001 20020826 5 2090
A00001 20021221 8 3038
A00002 20020112 1 341
A00002 20020208 12 4812
A00002 20020726 9 3379
A00002 20020822 10 4013
A00002 20021225 9 3532
A00003 20020727 8 1983
A00003 20020813 9 2898
A00003 20021008 11 4110
A00004 20020214 1 365
A00004 20020415 9 4349
A00004 20020625 13 5268
A00004 20020810 5 1805
A00004 20021014 2 612
A00004 20021016 11 3410
Example 1. Calculate the total amount on quantity purchased.
e.g. xtcal -c '$amount*$quantity' -a total amount -i dat.xt -o rsl.xt
Output: -rsl.xt
-
<body><![CDATA[
A00001 20020826 5 2090 10450
A00001 20021221 8 3038 24304
A00002 20020112 1 341 341
A00002 20020208 12 4812 57744
A00002 20020726 9 3379 30411
A00002 20020822 10 4013 40130
A00002 20021225 9 3532 31788
A00003 20020727 8 1983 15864
A00003 20020813 9 2898 26082
A00003 20021008 11 4110 45210
A00004 20020214 1 365 365
A00004 20020415 9 4349 39141
A00004 20020625 13 5268 68484
A00004 20020810 5 1805 9025
A00004 20021014 2 612 1224
A00004 20021016 11 3410 37510
Example 2. Calculate the average amount per visit and round off the result to 1 decimal place.
e.g. xtcal -c 'round($amount/$frequency of visit,0.1)' -a amount par visit -i dat.xt -o rsl.xt
Output: -rsl.xt
-
<body><![CDATA[
A00001 66664 2 33332
A00002 659157 5 131831.4
A00003 251748 3 83916
A00004 648169 6 108028.2
A00005 65585 2 32792.5
A00006 28472 2 14236
A00007 404576 6 67429.3
A00008 142538 2 71269
A00009 689392 6 114898.7
A00010 1777520 5 355504
SEE ALSO
xtcount(1),
xtstatistics(1)
For complete documentation and tutorial of xtcal and other commands, please visit
http://musashien.sourceforge.net.
BUG REPORT
musashi@adm.osaka-sandai.ac.jp
If you find a bug in xtcal, please send an electronic mail to
musashi@adm.osaka-sandai.ac.jp.
Before sending a bug report, please verify that you have the lastest version of
MUSASHI.
Read this manual carefully to ensure the error is not caused by a quirk in the language.
AUTHORS
Yukinobu Hamuro, Naoki Katoh, Katsutoshi Yada, Stephane Cheung
Index
- NAME
-
- SYNOPSIS
-
- DESCRIPTION
-
- PARAMETERS
-
- FILE OPTIONS
-
- USAGE
-
- SEE ALSO
-
- BUG REPORT
-
- AUTHORS
-
This document was created by
man2html,
using the manual pages.
Time: 22:43:53 GMT, June 24, 2003