miller/docs6/docs/reference-dsl.md.in

62 lines
1.8 KiB
Markdown

# DSL reference: overview
## Overview
Here's comparison of verbs and `put`/`filter` DSL expressions:
Example:
GENMD_RUN_COMMAND
mlr stats1 -a sum -f x -g a data/small
GENMD_EOF
* Verbs are coded in Go
* They run a bit faster
* They take fewer keystrokes
* There is less to learn
* Their customization is limited to each verb's options
Example:
GENMD_RUN_COMMAND
mlr put -q '@x_sum[$a] += $x; end{emit @x_sum, "a"}' data/small
GENMD_EOF
* You get to write your own DSL expressions
* They run a bit slower
* They take more keystrokes
* There is more to learn
* They are highly customizable
Please see [Verbs Reference](reference-verbs.md) for information on verbs other than `put` and `filter`.
The essential usages of `mlr filter` and `mlr put` are for record-selection and record-updating expressions, respectively. For example, given the following input data:
GENMD_RUN_COMMAND
cat data/small
GENMD_EOF
you might retain only the records whose `a` field has value `eks`:
GENMD_RUN_COMMAND
mlr filter '$a == "eks"' data/small
GENMD_EOF
or you might add a new field which is a function of existing fields:
GENMD_RUN_COMMAND
mlr put '$ab = $a . "_" . $b ' data/small
GENMD_EOF
The two verbs `mlr filter` and `mlr put` are essentially the same. The only differences are:
* Expressions sent to `mlr filter` must end with a boolean expression, which is the filtering criterion;
* `mlr filter` expressions may not reference the `filter` keyword within them; and
* `mlr filter` expressions may not use `tee`, `emit`, `emitp`, or `emitf`.
All the rest is the same: in particular, you can define and invoke functions and subroutines to help produce the final boolean statement, and record fields may be assigned to in the statements preceding the final boolean statement.
There are more details and more choices, of course, as detailed in the following sections.