Given file.csv:
a,b,c
1,2,3
How can mlr be made to output:
a,b,c
1,2,c
Using the label name of $c without knowing in advance that $c contains the letter "c"?
Note: correct answer must use mlr only.
Given file.csv:
a,b,c
1,2,3
How can mlr be made to output:
a,b,c
1,2,c
Using the label name of $c without knowing in advance that $c contains the letter "c"?
Note: correct answer must use mlr only.
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