Reputation: 1071
I have a tab delim file
1A 865 508 512 0 2 0 0
1B 0 0 0 0 0 0 1
I need to divide every col from $3
on by $2
and multiply the resulting by 100 line by line, printing NA where division by 0.
So that I have
1A 865 58.73 59.19 0.00 0.23 0.00 0.00 0.00 0.00
1B 0 NA NA NA NA NA NA NA NA
I tried
awk 'BEGIN{FS=OFS="\t"} FNR>1 {for(i=3;i<=NR;i++) $i={print $0"\t"($2?$i/$2*100:"NaN")1}'
but I get "unexpected newline or end of string"
Upvotes: 1
Views: 118
Reputation: 133700
Could you please try following based on your shown samples only. Basically you need to make 2 major changes here, 1st your loop should run till value of NF
NOT NR
, 2nd- you are using print while assigning current field's value which we don't want to do. I have written this on mobile so haven't tested it yet but should work I believe.
awk '
BEGIN{
FS=OFS="\t"
}
FNR>1{
for(i=3;i<=NF;i++){
$i=($2?($i/$2)*100:"NaN")
}
}
1' Input_file
Upvotes: 3