Output a header label in data field in miller

Edited answer

Hi, you could use this script

mlr --csv put 'if (NR == 1) {
counter=1;
  for (key in $*) {
    if (counter == 3) {
    $[key]=key;
    }
    counter += 1;
  }
}' input.csv

And as output you will have:

a,b,c
1,2,c

NR == 1 to have the first row, and counter == 3 to get the third field.


Simply with awk:

awk 'BEGIN{ FS=OFS="," }{ (NR == 1)? c=$NF : $NF=c }1' file.csv

Sample output:

a,b,c
1,2,c

miller v5.6.0 allows the use of $[[fieldno]] to refer to the value of the name of field number "fldno", so in your case field 3's name is $[[3]].

    mlr --csv put '$c = $[[3]]' file.csv