Skip to main content

## Expression Language

This page describes the language used for expressions by eval and other CSVfix commands.

The following data types are supported:

Data Type | Description
string | All expression values are stored as strings and can be treated as strings. Strings are denoted by either single or double quotes.
number | A value can be treated as a number if it contains a valid real or integer number.
boolean | All values can be treated as booleans. An empty string or a numeric value of zero are taken to be false, all other values are true.

The following operators are supported:

Operators | Description
* / % + - | Standard arithmetic binary operators. The operands must be numbers.
&& || | Boolean AND and OR operators - the operands may be of any type but will be treated as booleans. Note there is no NOT operator - see the not() function. also, CSVfix does not currently support short-circuited evaluation using these operators. This means that you cannot write expressions like this: $1 <> 0 && $2 / $1 as you will get a divide-by-zero error if $1 contains zero.
. | String concatenation - operands may be of any type but will be treated as strings.
== <> != < > <= >= | Comparison operators - the operands may be of any type. If both operands can be interpreted as numbers then numeric comparison is performed, otherwise a lexical comparison is used.

The following functions are available.

Function | Description
abs(num) | Returns the absolute (positive) value of num, which must be numeric.
bool(val) | Converts val to the boolean values 1 (true) or 0 (false)
day(d) | If d is a date in ISO YYYY-MM-DD format, returns the day part, otherwise returns empty string.
env(name) | Returns the value of the named environment variable, or an empty string if no such variable exists.
errorif(cond,rc,msg) | If the first parameter evaluates to true, write msg to standard error and exit CSVfix immediately with return code rc.
field(i) | Returns the i'th field in the current CSV input record. If the index specified is less than 1, or greater than the number of fields in the current row, returns the empty string.
find(regex) | Try to match each field in the current CSV record with regex, returning the numeric index of the first match, or 0 if no match is found.
if(test,v1,v2) | Evaluates the expression test (which itself may consist of expressions and functions) as a boolean value - if the evaluation is true returns the result of evaluating the expression v1, else it returns the result of evaluating v2. CSVfix does not currently support short-circuited evaluation, so both v1 and v2 must be valid expressions - the problems this causes can partially be mitigated by using the -if option of the eval command.
index(s,list) | Returns 1-based index of s in a comma-separated list. If s is not in the list, returns zero.
int(num) | Returns the integer part of num, which must be numeric. For example, int(12.34) would return 12.
isdate(d) | Returns true if d is a date in ISO YYYY-MM-DD format.
isempty(str) | Returns true if the string str contains only whitespace or is the empty string, returns false otherwise.
isint(str) | Returns true if the string str contains an integer, false otherwise.
isnum(str) | Tests if str is a valid number (either integer or real).
len(str) | Return length of string.
lower(str) | Returns the string str converted to lower-case.
match(str,regex) | Returns true if str matches regular expression.
max(a,b) | Returns the larger of the pair a,b. If both can be converted to numbers, numeric comparison is performed, otherwise string comparison is used. For example, max(42,666) would return 666, and max('foo','bar') would return 'foo'.
min(a,b) | As for max(), but returns minimum value.
month(d) | If d is a date in ISO YYYY-MM-DD format, returns the month part, otherwise returns empty string.
not(bool) | Inverts the boolean sense of bool, which may be of any type.
pick(i,list) | Picks the 1-based entry in the comma-separated list using i as index. If i is less than one or greater than the number of entries in the list, returns the empty string.
pos(s1,s2) | Returns the 1-based position of the string s2 in s1, or zero if s1 does not contain s2.
random() | Returns random real number in the range 0.0 <= N < 1.0. The generator can be seeded woith the -seed command line option.
round(num,places) | Returns the number num, rounded to places decimal places.
sign(num) | Returns the sign of num. Negative numbers return -1, positive numbers 1, zero returns 0.
streq(s1,s2) | Returns true if string s1 is identical to s2, ignoring case differences.
substr(s,start,n) | Returns a substring of s starting at start and consisting of a maximum of n characters.
trim(str) | Returns the string str trimmed of leading and trailing whitespace.
upper(str) | Returns the string str converted to upper-case.
year(d) | If d is a date in ISO YYYY-MM-DD format, returns the year part, otherwise returns empty string.

The following read-only variables are set before an expression is evaluated. Variable names are case-insensitive.

Variables | Description
$1 $2 ... $N | Values of the fields in the current CSV input row. If a field variable is used that represents a field that does not exist in the input row, it evaluates as the empty string. Note you will probably need to quote eval command options that use field variables to prevent them being interpreted as shell script variables.
$fields | The number of fields in the current input row.
$file | The name of the current input file.
$line | Line number of the current input line in the current file.

Created with the Personal Edition of HelpNDoc: Free help authoring environment

####