1

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
Ben Carlson
  • 1,053
  • 2
  • 10
  • 18

2 Answers2

3

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])}}'
aborruso
  • 4,938
  • 3
  • 23
  • 40
3

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)}}'
Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • 2
    @Favadona This is an excellent solution and probably what I will use in the future since it is so simple. The original questions asked for an answer using miller so I will leave aborruso's answer as the accepted answer. – Ben Carlson Apr 11 '22 at 14:13