Skip to content

Commit 8a84388

Browse files
authored
Add docs example on custom record-output formatting (#1989)
1 parent 5815ab1 commit 8a84388

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (NR > 1) { print }
2+
print "Shape is " . $shape;
3+
print "Color is " . $color;
4+
if ($w > 0.5) {
5+
print "W HAS EXCEEDED THRESHOLD: " . $w
6+
} else {
7+
print "W is within threshold: " . $w
8+
}

docs/src/record-templating-example.mlr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ $* = {
22
"color_shape": $color . " " . $shape,
33
"flags": {
44
"iflag": $flag == 1,
5-
"wflag": $w > 3.0,
5+
"w_exceeded": $w > 0.5,
66
},
77
"uvwx": [$u, $v, $w, $x],
88
}

docs/src/reference-dsl-output-statements.md

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5656
Note 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

@@ -91,7 +93,7 @@ $* = {
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

146148
See 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

150192
The `dump` statement is for printing expressions, including maps, directly to stdout/stderr, respectively:

docs/src/reference-dsl-output-statements.md.in

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ For example, if you used `-o csv` then these new records will appear in CSV form
3838
* See below for more examples.
3939

4040
Note that `dump`, `edump`, `print`, and `eprint` don't output records that participate in
41-
`then`-chaining: rather, they're just immediate prints to stdout/stderr. You can use these to print
42-
arbitrary things, like `"Hello, world!"`, or, you can use them to format your data in a custom
43-
format of your choice. In particular, you can use this to implement an output format that Miller
44-
doesn't handle out of the box.
41+
`then`-chaining: rather, they're just immediate prints to stdout/stderr.
42+
43+
You can use these to print arbitrary things, like `"Hello, world!"`, or, you can use them to format
44+
your data in [custom output format](#custom-output-formats-with-print-statements) of your choice. In
45+
particular, you can use this to implement an output format that Miller doesn't handle out of the
46+
box.
4547

4648
## Modifying the current record by assignment
4749

@@ -94,6 +96,22 @@ GENMD-EOF
9496

9597
See also [Redirected-output statements](reference-dsl-output-statements.md#redirected-output-statements) for examples.
9698

99+
## Custom output formats with print statements
100+
101+
Be sure to use `put -q` to silence the record stream, so that all output is controlled by your print statements:
102+
103+
GENMD-RUN-COMMAND
104+
cat data/colored-shapes-2.csv
105+
GENMD-EOF
106+
107+
GENMD-RUN-COMMAND
108+
cat custom-record-formatting.mlr
109+
GENMD-EOF
110+
111+
GENMD-RUN-COMMAND
112+
mlr -i csv --from data/colored-shapes-2.csv put -q -f custom-record-formatting.mlr
113+
GENMD-EOF
114+
97115
## Dump statements
98116

99117
The `dump` statement is for printing expressions, including maps, directly to stdout/stderr, respectively:

0 commit comments

Comments
 (0)