eval

Commands ››
Parent Previous Next

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, pipe it through the order command.

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 single quotes.


See also: edit, map, summary

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.



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: Full featured Help generator