docs/src/scripting.md
Suppose you are often doing something like
<pre class="pre-highlight-in-pair"> <b>mlr --icsv --opprint \</b> <b>filter '$quantity != 20' \</b> <b>then count-distinct -f shape \</b> <b>then fraction -f count \</b> <b>example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> shape count count_fraction triangle 3 0.3 square 4 0.4 circle 3 0.3 </pre>Typing this out can get a bit old, if the only thing that changes for you is the filename. Some options include:
#!/bin/sh which invokes Miller as part of the shell-script body.#!/usr/bin/env mlr -s which invokes Miller.myflags.txt), then mlr -s myflags-txt filename-which-varies.csv.Let's look at examples of each.
A shell-script option:
<pre class="pre-highlight-in-pair"> <b>cat example-shell-script</b> </pre> <pre class="pre-non-highlight-in-pair"> #!/bin/bash mlr --c2p \ filter '$quantity != 20' \ then count-distinct -f shape \ then fraction -f count \ -- "$@" </pre>Key points here:
-- before "$@" at the end so that main-flags like --json won't be confused for options to the fraction verb."$@" at the end which means "all remaining arguments to the script".chmod +x example-shell-script after you create one of these.Then you can do
<pre class="pre-highlight-in-pair"> <b>./example-shell-script example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> shape count count_fraction triangle 3 0.3 square 4 0.4 circle 3 0.3 </pre> <pre class="pre-highlight-in-pair"> <b>cat example.csv | ./example-shell-script</b> </pre> <pre class="pre-non-highlight-in-pair"> shape count count_fraction triangle 3 0.3 square 4 0.4 circle 3 0.3 </pre> <pre class="pre-highlight-in-pair"> <b>./example-shell-script --ojson example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> [ { "shape": "triangle", "count": 3, "count_fraction": 0.3 }, { "shape": "square", "count": 4, "count_fraction": 0.4 }, { "shape": "circle", "count": 3, "count_fraction": 0.3 } ] </pre> <pre class="pre-highlight-in-pair"> <b>./example-shell-script --ojson then filter '$count == 3' example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> [ { "shape": "triangle", "count": 3, "count_fraction": 0.3 }, { "shape": "circle", "count": 3, "count_fraction": 0.3 } ] </pre>etc.
Here instead of putting #!/bin/bash on the first line, we can put mlr directly:
Points:
chmod +x example-mlr-s-script after you create one of these.mlr since that's present on line 1.-- or "$@".# to end of line is stripped out. If for any reason you need to suppress this, please use mlr --s-no-comment-strip in place of mlr -s.Then you can do
<pre class="pre-highlight-in-pair"> <b>./example-mlr-s-script example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> shape count count_fraction triangle 3 0.3 square 4 0.4 circle 3 0.3 </pre> <pre class="pre-highlight-in-pair"> <b>cat example.csv | ./example-mlr-s-script</b> </pre> <pre class="pre-non-highlight-in-pair"> shape count count_fraction triangle 3 0.3 square 4 0.4 circle 3 0.3 </pre> <pre class="pre-highlight-in-pair"> <b>./example-mlr-s-script --ojson example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> [ { "shape": "triangle", "count": 3, "count_fraction": 0.3 }, { "shape": "square", "count": 4, "count_fraction": 0.4 }, { "shape": "circle", "count": 3, "count_fraction": 0.3 } ] </pre> <pre class="pre-highlight-in-pair"> <b>./example-mlr-s-script --ojson then filter '$count == 3' example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> [ { "shape": "triangle", "count": 3, "count_fraction": 0.3 }, { "shape": "circle", "count": 3, "count_fraction": 0.3 } ] </pre>Both the previous options require executable mode with chmod, and a shebang
line with #!..., which are unixisms.
One of the nice features of mlr -s is it can be done without a shebang line,
and this works fine on Windows. For example:
Points:
#! line isn't needed. (But you can include a #! line; mlr -s will simply see it as a comment line.)-- or "$@".Then you can do
<pre class="pre-highlight-in-pair"> <b>mlr -s example-mlr-s-script-no-shebang example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> shape count count_fraction triangle 3 0.3 square 4 0.4 circle 3 0.3 </pre> <pre class="pre-highlight-in-pair"> <b>mlr -s example-mlr-s-script-no-shebang --ojson example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> [ { "shape": "triangle", "count": 3, "count_fraction": 0.3 }, { "shape": "square", "count": 4, "count_fraction": 0.4 }, { "shape": "circle", "count": 3, "count_fraction": 0.3 } ] </pre> <pre class="pre-highlight-in-pair"> <b>mlr -s example-mlr-s-script-no-shebang --ojson then filter '$count == 3' example.csv</b> </pre> <pre class="pre-non-highlight-in-pair"> [ { "shape": "triangle", "count": 3, "count_fraction": 0.3 }, { "shape": "circle", "count": 3, "count_fraction": 0.3 } ] </pre>and so on. See also Miller on Windows.