unix-text-processingmiller

Convert field names to lower case using miller


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

Solution

  • 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])}}'