miller/doc/content-for-feature-comparison.html
2016-02-12 18:26:52 -05:00

69 lines
2.6 KiB
HTML

POKI_PUT_TOC_HERE
<h1>File-format awareness</h1>
Miller respects CSV headers. If you do <tt>mlr --csv cat *.csv</tt> then the header line is written once:
<table><tr>
<td>
POKI_RUN_COMMAND{{cat data/a.csv}}HERE
</td>
<td>
POKI_RUN_COMMAND{{cat data/b.csv}}HERE
</td>
<td>
POKI_RUN_COMMAND{{mlr --csv cat data/a.csv data/b.csv}}HERE
</td>
<td>
POKI_RUN_COMMAND{{mlr --csv sort -nr b data/a.csv data/b.csv}}HERE
</td>
</tr></table>
Likewise with <tt>mlr sort</tt>, <tt>mlr tac</tt>, and so on.
<h1>awk-like features: mlr filter and mlr put</h1>
<ul>
<li/> <tt>mlr filter</tt> includes/excludes records based on a filter
expression, e.g. <tt>mlr filter '$count &gt; 10'</tt>.
<li/> <tt>mlr put</tt> adds a new field as a function of others, e.g. <tt>mlr
put '$xy = $x * $y'</tt> or <tt>mlr put '$counter = NR'</tt>.
<li/> The <tt>$name</tt> syntax is straight from <tt>awk</tt>&rsquo;s <tt>$1 $2
$3</tt> (adapted to name-based indexing), as are the variables <tt>FS</tt>,
<tt>OFS</tt>, <tt>RS</tt>, <tt>ORS</tt>, <tt>NF</tt>, <tt>NR</tt>, and
<tt>FILENAME</tt>. The <tt>ENV[...]</tt> syntax is from Ruby.
<li/> While <tt>awk</tt> functions are record-based, Miller subcommands (or
functions, if you like) are stream-based: each of them maps a stream of records
into another stream of records.
<li/> Unlike <tt>awk</tt>, Miller doesn&rsquo;t allow you to define new functions.
Its domain-specific languages are limited to the <tt>filter</tt> and
<tt>put</tt> syntax. Futher programmability comes from chaining with
<tt>then</tt>.
<li/> Unlike with <tt>awk</tt>, all variables are stream variables and all
functions are stream functions. This means <tt>NF</tt>, <tt>NR</tt>, etc.
change from one line to another, <tt>$x</tt> is a label for field <tt>x</tt> in
the current record, and the input to <tt>sqrt($x)</tt> changes from one record
to the next. Miller doesn&rsquo;t let you set <tt>sum=0</tt> before
records are read, then update that sum on each record, then print its value at the
end. (However, do see <tt>mlr step -a rsum</tt> in the
POKI_PUT_LINK_FOR_PAGE(reference.html)HERE) page.)
<li/> Miller is faster than <tt>awk</tt>, <tt>cut</tt>, and so on (depending on
platform; see also POKI_PUT_LINK_FOR_PAGE(performance.html)HERE). In
particular, Miller&rsquo;s DSL syntax is parsed into C control structures at
startup time, with the bulk data-stream processing all done in C.
</ul>
<h1>See also</h1>
<p/>See POKI_PUT_LINK_FOR_PAGE(reference.html)HERE for more on Miller&rsquo;s
subcommands <tt>cat</tt>, <tt>cut</tt>, <tt>head</tt>, <tt>sort</tt>,
<tt>tac</tt>, <tt>tail</tt>, <tt>top</tt>, and <tt>uniq</tt>, as well as awk-like
<tt>mlr filter</tt> and <tt>mlr put</tt>.