Skip to content

Commit aea6f61

Browse files
authored
FAQ entry for #285: carriage returns in field names (#953)
1 parent 228f734 commit aea6f61

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

docs/src/operating-on-all-fields.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,39 @@ a_b_c def g_h_i
7575
9987 3312 4543
7676
</pre>
7777

78+
## Bulk rename of fields with carriage returns
79+
80+
The previous example isn't sufficient when there are carriage returns in the field names. Here we can use
81+
the [Miller programming language](miller-programming-language.md):
82+
83+
<pre class="pre-highlight-in-pair">
84+
<b>cat data/header-lf.csv</b>
85+
</pre>
86+
<pre class="pre-non-highlight-in-pair">
87+
"field
88+
A",field B
89+
1,2
90+
3,3
91+
6,6
92+
</pre>
93+
94+
<pre class="pre-highlight-in-pair">
95+
<b>mlr --csv --from data/header-lf.csv put '</b>
96+
<b> map inrec = $*;</b>
97+
<b> $* = {};</b>
98+
<b> for (oldkey, value in inrec) {</b>
99+
<b> newkey = clean_whitespace(gsub(oldkey, "\n", " "));</b>
100+
<b> $[newkey] = value;</b>
101+
<b> }</b>
102+
<b>'</b>
103+
</pre>
104+
<pre class="pre-non-highlight-in-pair">
105+
field A,field B
106+
1,2
107+
3,3
108+
6,6
109+
</pre>
110+
78111
## Search-and-replace over all fields
79112

80113
How to do `$name = gsub($name, "old", "new")` for all fields?

docs/src/operating-on-all-fields.md.in

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ GENMD-RUN-COMMAND
2828
mlr --icsv --opprint put -f data/bulk-rename-for-loop.mlr data/spaces.csv
2929
GENMD-EOF
3030

31+
## Bulk rename of fields with carriage returns
32+
33+
The previous example isn't sufficient when there are carriage returns in the field names. Here we can use
34+
the [Miller programming language](miller-programming-language.md):
35+
36+
GENMD-RUN-COMMAND
37+
cat data/header-lf.csv
38+
GENMD-EOF
39+
40+
GENMD-RUN-COMMAND
41+
mlr --csv --from data/header-lf.csv put '
42+
map inrec = $*;
43+
$* = {};
44+
for (oldkey, value in inrec) {
45+
newkey = clean_whitespace(gsub(oldkey, "\n", " "));
46+
$[newkey] = value;
47+
}
48+
'
49+
GENMD-EOF
50+
3151
## Search-and-replace over all fields
3252

3353
How to do `$name = gsub($name, "old", "new")` for all fields?

0 commit comments

Comments
 (0)