Skip to content

Useful for a FAQ item? Apply DSL to CSV field names #285

@aborruso

Description

@aborruso

Hi,
I'm sure that's a direct and better way to do it, but I do not find it :(

I have in example this CSV with a carriage return in one of its field names, and my goal is to use DSL to replace it with a space.

"field 
A",field B
1,2
3,3
6,6

To do it you could run

mlr -I --csv -N put -S 'if (NR == 1) {for (k in $*) {$[k] = clean_whitespace(gsub($[k], "\n", " "))}}' input.csv
  • using -N (a great new feature) you apply --implicit-csv-header and --headerless-csv-output and the row with field names is no more the heading, but the first data row;
  • then I apply a search and replace only to the first row (using NR == 1);
  • and at the end, since we have --headerless-csv-output, the first row becomes again the heading.

The result will be

field A,field B
1,2
3,3
6,6

Probably not so useful, but it has saved my day.

Thank you always @johnkerl

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions