Cannot get the correct ans. Using awk in taking average


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cannot get the correct ans. Using awk in taking average
# 1  
Old 06-21-2013
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,

Code:
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
Code:
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

Code:
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

Code:
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

Code:
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 https://www.unix.com/shell-programmin...ect-order.html

Last edited by Scott; 06-21-2013 at 06:43 AM.. Reason: Code tags for code too!
# 2  
Old 06-21-2013
Code:
 
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[i]/3;}}' file1 file2 file3

# 3  
Old 06-21-2013
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
# 4  
Old 06-21-2013
This is how you need to iterate the same logic
Code:
$ 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

# 5  
Old 06-21-2013
Hi Raja,

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

Code:
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
# 6  
Old 06-21-2013
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


Code:
$ 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.

Last edited by rajamadhavan; 06-21-2013 at 06:50 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command line arguments not taking

# more minusf.awk #!/bin/awk -f BEGIN { FS=":"; } { if ( $2 == "" ) { print $1 ": no password!"; } } # ./minusf.awk aa aa aa aa awk: can't open aa (6 Replies)
Discussion started by: sri.phani
6 Replies

2. Shell Programming and Scripting

Taking inputs for awk

Hi, i want to print 2nd column value with the below script. I need to take input of the string i need to search in that file and file name. How can i take these two as inputs? using read command? Getting error for below script. echo "enter SID" read SID echo "enter filename" read filename... (8 Replies)
Discussion started by: sam_bd
8 Replies

3. UNIX for Dummies Questions & Answers

awk solution for taking bins

Hi all, I'm looking for an awk solution for taking bins of data set. For example, if I have two columns of data that I wish to use for a scatter plot, and it contains 5 million lines, how can I take averages of every 100 points, 1000, 10000 etc... The idea is to take bins of the 5,000,000 points... (7 Replies)
Discussion started by: torchij
7 Replies

4. UNIX for Dummies Questions & Answers

Taking the average of two columns and printing it on a new column

Hi, I have a space delimited text file that looks like the following: Aa 100 200 Bb 300 100 Cc X 500 Dd 600 X Basically, I want to take the average of columns 2 and 3 and print it in column 4. However if there is an X in either column 2 or 3, I want to print the non-X value. Therefore... (11 Replies)
Discussion started by: evelibertine
11 Replies

5. UNIX for Dummies Questions & Answers

Taking a average of a column of numbers

Hey all, I am relatively poor at programming and unfortunately don't have time to read about programming at this current moment. I wanted to be able to run a simple command to read a column of numbers in a file and give me the average of those numbers. In addition if I could specify the... (2 Replies)
Discussion started by: Leonidsg
2 Replies

6. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

7. Shell Programming and Scripting

Help in extracting multiple files and taking average at same time

Hi, I have 20 files which have respective 50 lines with different values. I would like to process each line of the 50 lines in these 20 files one at a time and do an average of 3rd field ($3) of these 20 files. This will be output to an output file. Instead of using join to generate whole... (8 Replies)
Discussion started by: ahjiefreak
8 Replies

8. Shell Programming and Scripting

Average in awk

Hi I am looking for an awk script which can compute average of all the fields every 5th line. The file looks: A B C D E F G H I J K L M 1 18 13 14 12 14 13 11 12 12 15 15 15 2 17 17 13 13 13 12 12 11 12 14 15 14 3 16 16 12 12 12 11 11 12 11 16 14 13 4 15 15 11 11 11 12 11 12 11... (6 Replies)
Discussion started by: saint2006
6 Replies

9. Shell Programming and Scripting

how to average in awk

Hi, I have the data like this $1 $2 1 12 2 13 3 14 4 12 5 12 6 12 7 13 8 14 9 12 10 12 i want to compute average of $1 and $2 every 5th line (1-5 and 6-10) Please help me with awk Thank you (4 Replies)
Discussion started by: saint2006
4 Replies

10. Shell Programming and Scripting

Bash/AWK Newbie taking on more than he can chew.

A few questions: I'm trying to use Bash (although I'm not against using AWK) to try to accomplish a few things, but I'm stumped on a few points. I'm learning most of the basics quickly: but there are a few things I can't figure out. 1. I'm trying to count the number of .txt files in a... (3 Replies)
Discussion started by: Asylus
3 Replies
Login or Register to Ask a Question