Convert field names to lower case using miller

294 Views Asked by At

I would like to use miller (mlr) to convert column names to lower case. The closest I get is using the rename verb with a regular expression. \L should change the case, but instead the the column names are getting prefixed by "\L".

I'm using macOS Catalina and miller 5.10.0

echo -e 'A,B,C\n1,2,3' | mlr --csv --opprint rename -r '(.*),\L\1'

prints

\LA \LB \LC
1   2   3

But I would like it to print

a b c
1 2 3
2

There are 2 best solutions below

0
aborruso On BEST ANSWER

Two examples ways:

echo -e 'A,B,C\n1,2,3' | mlr --csv put '
  map inrec = $*;
  $* = {};
  for (oldkey, value in inrec) {
    newkey = tolower(oldkey);
    $[newkey] = value;
  }
'

or

echo -e 'A,B,C\n1,2,3' | mlr --csv -N put -S 'if (NR == 1) {for (k in $*) {$[k] = tolower($[k])}}'
1
Fravadona On

Sometimes, standard tools are easier to use:

echo -e 'A,B,C\n1,2,3' | awk 'NR == 1 { $0 = tolower($0) } 1'

UPDATE

with Miller:

echo -e 'A,B,C\n1,2,3' |
mlr --csv -N put 'NR == 1 {for (k,v in $*) {$[k] = tolower(v)}}'