Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAQ entry for #285: carriage returns in field names #953

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/src/operating-on-all-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ a_b_c def g_h_i
9987 3312 4543
</pre>

## Bulk rename of fields with carriage returns

The previous example isn't sufficient when there are carriage returns in the field names. Here we can use
the [Miller programming language](miller-programming-language.md):

<pre class="pre-highlight-in-pair">
<b>cat data/header-lf.csv</b>
</pre>
<pre class="pre-non-highlight-in-pair">
"field
A",field B
1,2
3,3
6,6
</pre>

<pre class="pre-highlight-in-pair">
<b>mlr --csv --from data/header-lf.csv put '</b>
<b> map inrec = $*;</b>
<b> $* = {};</b>
<b> for (oldkey, value in inrec) {</b>
<b> newkey = clean_whitespace(gsub(oldkey, "\n", " "));</b>
<b> $[newkey] = value;</b>
<b> }</b>
<b>'</b>
</pre>
<pre class="pre-non-highlight-in-pair">
field A,field B
1,2
3,3
6,6
</pre>

## Search-and-replace over all fields

How to do `$name = gsub($name, "old", "new")` for all fields?
Expand Down
20 changes: 20 additions & 0 deletions docs/src/operating-on-all-fields.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ GENMD-RUN-COMMAND
mlr --icsv --opprint put -f data/bulk-rename-for-loop.mlr data/spaces.csv
GENMD-EOF

## Bulk rename of fields with carriage returns

The previous example isn't sufficient when there are carriage returns in the field names. Here we can use
the [Miller programming language](miller-programming-language.md):

GENMD-RUN-COMMAND
cat data/header-lf.csv
GENMD-EOF

GENMD-RUN-COMMAND
mlr --csv --from data/header-lf.csv put '
map inrec = $*;
$* = {};
for (oldkey, value in inrec) {
newkey = clean_whitespace(gsub(oldkey, "\n", " "));
$[newkey] = value;
}
'
GENMD-EOF

## Search-and-replace over all fields

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