The eval command provides the ability to perform arithmetic and string manipulation on CSV input fields using a simple expression-based language. Each CSV field in a CSV record is assigned to a positional parameter, with $1 being the first, $2 the second, and so on. You can then use simple arithmetic expressions such as ($1 + $2)/2, which calculates the average of the first two fields. The results of these expressions are appended to the output, or replace existing fields if the -r flag is used. If you want to remove the base fields that eval used from the output, use the -d option.
Note that (depending on your command shell), the field place holders $1, $2 etc. may have a special meaning which will interfere with correct expression evaluation. To be safe, always enclose the expression being evaluated in quotes - single quotes on Linux, double quotes on Windows.
A problem with the eval command is that the if() function in the expression language will always evaluate all of its parameters, which means you cannot write code like this:
csvfix eval -e 'if( $2 == 0, "divide by zero", $1 / $2 )
as the divide expression will result in an exception being thrown if $2 contains zero. You can use the -if option to get round this:
csvfix eval -if '$2 == 0' -e '"divide by zero"', -e '$1 / $2'
Here, if field $2 contains zero, the divide expression will never be evaluated, and instead the message "divide by zero" is output to the CSV stream.
Flag |
Req'd? |
Description |
-e expr |
No |
Specifies an expression to evaluate. The result of the evaluation will be added as a new field to the end of the output row. You may have more than one -e flag - each one adds a new field to the output. At least one of -e or -r must be specified. |
-r field,expr |
No |
As for -e, but replace specified field with result of expression evaluation. |
-d |
No |
Discard input data, resulting in output which only contains the results of evaluating -r or -e flags. This saves having to pipe the output through order, if you only want evaluation results. |
-if expr |
No |
Evaluate the expression. If this results in a true value, then evaluate the next -e option, if the result is false, skip the next -e option, and evaluate the one after that. |
The following example calculates the average of two temperatures in the minmax.csv file:
csvfix eval -e '($2 + $3)/2' data/minmax.csv
producing:
"2009-01-01","-5","2","-1.5"
"2009-01-02","-6","0","-3"
"2009-01-03","-5","2","-1.5"
"2009-01-02","-5","4","-0.5"
"2009-01-02","-3","6","1.5"
Created with the Personal Edition of HelpNDoc: Easily create EPub books