expr
Evaluates the expr language for each row.
Behavior:
- Parses and evaluates an expression against each row of input data.
- Default mode outputs only the expression result (original row data is not included).
- Supports arithmetic, string, logical operations, function calls, and lambda expressions.
- See
tva --help-exprfor a quick reference to the expr language and the detailed CLI instructions.
Input:
- Reads from files or standard input.
- Files ending in
.gzare transparently decompressed. - Use
stdinto explicitly read from stdin, this is different behavior from other commands. - Use
-rfor inline row data without file input.
Output:
- Default: outputs the evaluated result for each row.
- Use
-mflag to change output mode:eval(default),add,mutate,skip-null,filter.
Header behavior:
- Supports basic header mode. See
tva --help-headersfor details. - When headers are enabled, column names can be referenced with
@namesyntax. - The output header is determined by the expression:
as @namebinding: usesnameas the header@column_namereference: usescolumn_nameas the header@1with input headers: uses the first input column name- Other expressions: uses the formatted last expression string
Examples:
-
Simple arithmetic
tva expr -E '2 + 3 * 4' -
Calculate total from price and quantity
tva expr -H -E '@price * @qty' data.tsv -
Named output column with
astva expr -H -E '@price * @qty as @total' data.tsv -
Chain functions with pipe
tva expr -H -E '@name | trim() | upper()' data.tsv -
Conditional expression
tva expr -H -E 'if(@score >= 70, "pass", "fail")' data.tsv -
Add new column(s) to original row
tva expr -H -m extend -E '@price * @qty as @total' data.tsv -
Mutate (modify) existing column value
tva expr -H -m mutate -E '@age + 1 as @age' data.tsv -
Filter rows by condition
tva expr -H -m filter -E '@age > 25' data.tsv -
Skip null results
tva expr -H -m skip-null -E 'if(@score >= 70, @name, null)' data.tsv -
Test with inline row data
tva expr -n 'price,qty' -r '100,2' -E '@price * @qty'