miller

How to use positional field names with Miller `cut` verb


I want to use Miller cli to cut records based on numbered position, not specific field name.

For example I want only the 2 field (t), I've tried the following but no dice.

echo s=green,t=red,a=3,b=4 | mlr cut -f '$[[2]]'

I guess the Positional field name in the docs are for put and filter verbs only.


Solution

  • Yes positional field name are for put and filter verbs only, then you must use put and control structures.

    In example, if you want the fourth field you can run

    echo s=green,t=red,a=3,b=4 | mlr put -q '
      @i = 0;
      for (k, v in $*) {
        @i += 1;
        if (@i == 4) {
          emit {k: v};
          break;
        }
      }
    '
    

    to have b=4.