File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,39 @@ a_b_c def g_h_i
75
75
9987 3312 4543
76
76
</pre >
77
77
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
+
78
111
## Search-and-replace over all fields
79
112
80
113
How to do ` $name = gsub($name, "old", "new") ` for all fields?
Original file line number Diff line number Diff line change @@ -28,6 +28,26 @@ GENMD-RUN-COMMAND
28
28
mlr --icsv --opprint put -f data/bulk-rename-for-loop.mlr data/spaces.csv
29
29
GENMD-EOF
30
30
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
+
31
51
## Search-and-replace over all fields
32
52
33
53
How to do `$name = gsub($name, "old", "new")` for all fields?
You can’t perform that action at this time.
0 commit comments