Cannot get the correct ans. Using awk in taking average

Hi all,

I think so I�m getting the result is wrong, while using following awk commend,

colval=$(awk 'FNR>1 && NR==FNR{a[$2]=$4;next;} FNR>1 {a[$2]+=$4; print $2"\t"a[$2]/3}'
  filename_f.tsv filename_f2.tsv filename_f3.tsv)

echo $colval >> Result.tsv

it�s doing the condition 2 times, first result I�m getting wrong value when it occur 2nd time in tsv file it�s close to what is the result I actually want.

---------- Post updated at 12:45 PM ---------- Previous update was at 12:44 PM ----------

filename_f.tsv

Sr. No.	ID	Char	P1
11	14150524	Sa	39.88
12	12311440	Sa	0
13	12441731	Sa	111.66
14	15120599	Sa	69.97
15	21635123	Sa	149.99
16	9854892	Sa	27.06
17	14526541	Sa	67.05
18	10993779	Sa	99
19	15684120	Sa	106.99
20	6051457	Sa	249
21	10983989	Sa	149.97
22	8222030	Sa	59
23	10910428	Sa	237.2
24	8477371	Sa	125.72
25	14321003	Sa	28.92
26	15742934	Sa	129
27	4408441	Sa	125
28	4408440	Sa	125
29	4408439	Sa	125

filename_f2.tsv

Sr. No.	ID	Char	P1
11	14150524	Sa	39.88
12	12311440	Sa	0
13	12441731	Sa	111.66
14	15120599	Sa	69.97
15	21635123	Sa	148.99
16	9854892	Sa	26.39
17	14526541	Sa	67.45
18	10993779	Sa	97
19	15684120	Sa	104.99
20	6051457	Sa	229
21	10983989	Sa	147.97
22	8222030	Sa	59
23	10910428	Sa	147.59
24	8477371	Sa	125.72
25	14321003	Sa	28.92
26	15742934	Sa	129
27	4408441	Sa	125
28	4408440	Sa	125
29	4408439	Sa	125

filename_f3.tsv

Sr. No.	ID	Char	P1
11	14150524	Sa	37.88
12	12311440	Sa	0
13	12441731	Sa	121.66
14	15120599	Sa	70.17
15	21635123	Sa	159.99
16	9854892	Sa	27.06
17	14526541	Sa	68.05
18	10993779	Sa	99
19	15684120	Sa	106.99
20	6051457	Sa	149
21	10983989	Sa	109.97
22	8222030	Sa	59
23	10910428	Sa	257.2
24	8477371	Sa	127.2
25	14321003	Sa	26.92
26	15742934	Sa	129
27	4408441	Sa	124
28	4408440	Sa	123
29	4408439	Sa	122

Result.tsv

ID	P1_avge
14150524	26.59
12311440	0.00
12441731	74.44
15120599	46.65
21635123	99.66
9854892	17.82
14526541	44.83
10993779	65.33
15684120	70.66
6051457	159.33
10983989	99.31
8222030	39.33
10910428	128.26
8477371	83.81
14321003	19.28
15742934	86.00
4408441	83.33
4408440	83.33
4408439	83.33
14150524	39.21
12311440	0.00
12441731	114.99
15120599	70.04
21635123	152.99
9854892	26.84
14526541	67.52
10993779	98.33
15684120	106.32
6051457	209.00
10983989	135.97
8222030	59.00
10910428	214.00
8477371	126.21
14321003	28.25
15742934	129.00
4408441	124.67
4408440	124.33
4408439	124.00

---------- Post updated at 12:46 PM ---------- Previous update was at 12:45 PM ----------

Pls check the thread How to print the output in correct order?

 
awk 'FNR>1 && NR==FNR{a[$2]=$4;next;}
FNR>1{a[$2]+=$4}
END{for (i in a) {printf "%-20s\t%.2f\n",i,a/3;}}' file1 file2 file3

Hi pravin,

the result is printing different the value is different,
that is not in the order as like in input, and it is printing in vertical line

---------- Post updated at 01:34 PM ---------- Previous update was at 01:25 PM ----------

Hi all,

instead of awk command can we use some other commends to calculate and
print it in correct order

This is how you need to iterate the same logic

$ awk '(FNR>1 && NR==FNR){a[$2]+=$4;s=FNR;next;} (FNR>1 && NR==s+FNR){a[$2]+=$4;next;} FNR>1 {a[$2]+=$4; print $2"\t"a[$2]/3}' f1 f2 f3
952    133.333
124    143.333
950    90.6667
125    156.667
800    103.333

Hi Raja,

if i'm using the code which you posted, i'm getting following result, the Result with the input.

39.2133	11	14150524	Sa	37.88
0	12	12311440	Sa	0
114.993	13	12441731	Sa	121.66
70.0367	14	15120599	Sa	70.17
152.99	15	21635123	Sa	159.99
26.8367	16	9854892	Sa	27.06
67.5167	17	14526541	Sa	68.05
98.3333	18	10993779	Sa	99
106.323	19	15684120	Sa	106.99
209	20	6051457	Sa	149

---------- Post updated at 03:11 PM ---------- Previous update was at 03:09 PM ----------

Here i no need to print other values,
result only enough, even no need to print the ID also

---------- Post updated at 03:13 PM ---------- Previous update was at 03:11 PM ----------

Hi Raja
I think your code is giving correct ans. but it need to modify slightly to get only the result value

No. With same set of files, i am getting it correctly. Were the source files created in unix or windows ? You may want to do 'dos2unix' on the input files to ensure it doesnt have any ^M

$ awk '(FNR>1 && NR==FNR){a[$2]+=$4;s=FNR;next;} (FNR>1 && NR==s+FNR){a[$2]+=$4;next;} FNR>1 {a[$2]+=$4; print $2"\t"a[$2]/3}' f1 f2 f3
14150524    39.2133
12311440    0
12441731    114.993
15120599    70.0367
21635123    152.99
9854892    26.8367
14526541    67.5167
10993779    98.3333
15684120    106.323
6051457    209
10983989    135.97
8222030    59
10910428    213.997
8477371    126.213
14321003    28.2533
15742934    129
4408441    124.667
4408440    124.333
4408439    124

for just the result and no other fields, use only "print a[$2]/3" at the end.