@@ -54,10 +54,12 @@ For example, if you used `-o csv` then these new records will appear in CSV form
5454* See below for more examples.
5555
5656Note that ` dump ` , ` edump ` , ` print ` , and ` eprint ` don't output records that participate in
57- ` then ` -chaining: rather, they're just immediate prints to stdout/stderr. You can use these to print
58- arbitrary things, like ` "Hello, world!" ` , or, you can use them to format your data in a custom
59- format of your choice. In particular, you can use this to implement an output format that Miller
60- doesn't handle out of the box.
57+ ` then ` -chaining: rather, they're just immediate prints to stdout/stderr.
58+
59+ You can use these to print arbitrary things, like ` "Hello, world!" ` , or, you can use them to format
60+ your data in [ custom output format] ( #custom-output-formats-with-print-statements ) of your choice. In
61+ particular, you can use this to implement an output format that Miller doesn't handle out of the
62+ box.
6163
6264## Modifying the current record by assignment
6365
9193 "color_shape": $color . " " . $shape,
9294 "flags": {
9395 "iflag": $flag == 1,
94- "wflag ": $w > 3.0 ,
96+ "w_exceeded ": $w > 0.5 ,
9597 },
9698 "uvwx": [$u, $v, $w, $x],
9799}
@@ -106,15 +108,15 @@ $* = {
106108 "color_shape": "yellow triangle",
107109 "flags": {
108110 "iflag": true,
109- "wflag ": false
111+ "w_exceeded ": false
110112 },
111113 "uvwx": [0.632170, 0.988721, 0.436498, 5.798188]
112114},
113115{
114116 "color_shape": "red square",
115117 "flags": {
116118 "iflag": true,
117- "wflag ": false
119+ "w_exceeded ": true
118120 },
119121 "uvwx": [0.219668, 0.001257, 0.792778, 2.944117]
120122}
@@ -145,6 +147,46 @@ The `print` statement is perhaps self-explanatory, but with a few light caveats:
145147
146148See also [ Redirected-output statements] ( reference-dsl-output-statements.md#redirected-output-statements ) for examples.
147149
150+ ## Custom output formats with print statements
151+
152+ Be sure to use ` put -q ` to silence the record stream, so that all output is controlled by your print statements:
153+
154+ <pre class =" pre-highlight-in-pair " >
155+ <b >cat data/colored-shapes-2.csv</b >
156+ </pre >
157+ <pre class =" pre-non-highlight-in-pair " >
158+ color,shape,flag,i,u,v,w,x
159+ yellow,triangle,1,56,0.632170,0.988721,0.436498,5.798188
160+ red,square,1,80,0.219668,0.001257,0.792778,2.944117
161+ </pre >
162+
163+ <pre class =" pre-highlight-in-pair " >
164+ <b >cat custom-record-formatting.mlr</b >
165+ </pre >
166+ <pre class =" pre-non-highlight-in-pair " >
167+ if (NR > 1) { print }
168+ print "Shape is " . $shape;
169+ print "Color is " . $color;
170+ if ($w > 0.5) {
171+ print "W HAS EXCEEDED THRESHOLD: " . $w
172+ } else {
173+ print "W is within threshold: " . $w
174+ }
175+ </pre >
176+
177+ <pre class =" pre-highlight-in-pair " >
178+ <b >mlr -i csv --from data/colored-shapes-2.csv put -q -f custom-record-formatting.mlr</b >
179+ </pre >
180+ <pre class =" pre-non-highlight-in-pair " >
181+ Shape is triangle
182+ Color is yellow
183+ W is within threshold: 0.436498
184+
185+ Shape is square
186+ Color is red
187+ W HAS EXCEEDED THRESHOLD: 0.792778
188+ </pre >
189+
148190## Dump statements
149191
150192The ` dump ` statement is for printing expressions, including maps, directly to stdout/stderr, respectively:
0 commit comments