need an AWK code: put in parallel & average them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need an AWK code: put in parallel & average them
# 1  
Old 11-25-2009
need an AWK code: put in parallel & average them

Hi,

I have a data as follow:

1, 1.0 4.00
1, 2.0 4.00
1, 3.0 4.43
1, 4.0 4.40
1, 5.0 4.48
2, 1.0 0.00
2, 2.0 4.40
2, 3.0 3.40
2, 4.0 4.40
2, 5.0 1.47
3, 1.0 1.00
3, 2.0 3.40
3, 3.0 2.40
3, 4.0 2.40
3, 5.0 2.40


I want to print out in parallel:

1.0 4.00 0.00 1.00
2.0 4.00 4.40 3.40
3.0 4.43 3.40 2.40
4.0 4.40 4.40 2.40
5.0 4.48 1.47 2.40


Then, average each line (desired output):

1.0 1.66
2.0 3.93
3.0 3.41
4.0 3.73
5.0 2.78


Please, let me know how to write an AWK code to do above job.

Thanks
# 2  
Old 11-26-2009
Try:

Code:
awk  '{ arr[$2]=arr[$2]" "$3; } END {
for (i in arr) {
print i" "arr[i] >>"Parallel_out";
split(arr[i],ar);
printf "%s %.2f\n",i,(ar[1]+ar[2]+ar[3])/3 >>"avg_out";
}}'  < file

Output:

cat avg_out
Quote:
1.0 1.67
4.0 3.73
2.0 3.93
5.0 2.78
3.0 3.41
cat Parallel_out
Quote:
1.0 4.00 0.00 1.00
4.0 4.40 4.40 2.40
2.0 4.00 4.40 3.40
5.0 4.48 1.47 2.40
3.0 4.43 3.40 2.40

Last edited by dennis.jacob; 11-26-2009 at 12:55 AM.. Reason: Modified with output
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Help me Solaris 10&11 cpu load average states for 24 hours report

need to capture the following data on an hourly basis without cronjob scheduling in Solaris 5.10/5.11:- 1. load averages 2. Total no. of processes. 3. CPU state 4. Memory 5. Top 3 process details. any other third-party tool is available? (7 Replies)
Discussion started by: thoranam
7 Replies

2. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

3. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

4. Shell Programming and Scripting

Help with Associative array to sum & Average

Hi All, I got stuck up with shell script where i use awk. The scenario which i am working on is as below. I have a file text.txt with contents COL1 COL2 COL3 COL4 1 A 500 200 1 B 500 300 2 A 290 150 2 B 290 140 3 C 100 100 I could able to sum col 3 and col4 based on col1 using... (1 Reply)
Discussion started by: imsularif
1 Replies

5. Shell Programming and Scripting

How to put date range from a perl & sql script

Hi Guys, Can someone please help me on adding/inserting a variable date to an sql scipt? Basically I want to assign a 7 days date range. As shown below.. #!/usr/bin/perl use strict; use Env qw(ORACLE_HOME); my $SQLPLUS='/opt/oracle/product/10.1.0/db_1/bin/sqlplus -S... (1 Reply)
Discussion started by: pinpe
1 Replies

6. Shell Programming and Scripting

To find sum & average of 8th field

Hi Friends, I have many files like below. total,0.7%,0.0%,0.2%,0.0%,0.2%,0.7%,98.0% total,1.9%,0.0%,0.4%,0.0%,0.0%,6.8%,90.6% total,0.9%,0.0%,0.4%,0.0%,0.0%,0.0%,98.5% total,1.4%,0.0%,0.7%,0.0%,0.2%,2.9%,94.5% total,0.7%,0.0%,0.4%,0.0%,0.0%,0.9%,97.7%... (13 Replies)
Discussion started by: SunilB2011
13 Replies

7. UNIX for Dummies Questions & Answers

Where do i put ifconfig & route add for boot?

SCO v5.0.6 I figured out what command lines I need to get SSH working, but where do I put these command lines so they stick when booting? #ifconfig net0 ... netmask ... up #route add default ... I know I can add the default gateway to /etc/default/tcp, but... I would rather run a... (2 Replies)
Discussion started by: vdi_jeanne
2 Replies

8. UNIX for Dummies Questions & Answers

perl script dies altogether when I put an & after it

ignore (2 Replies)
Discussion started by: hairytorus
2 Replies

9. 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

10. 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
Login or Register to Ask a Question