Simple AWK script problem.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple AWK script problem.
# 1  
Old 08-12-2009
Simple AWK script problem.

Hi all,

I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below:
Code:
1 4.43
1 3.65
1 2.45
2 7.65
2 8.23
2 5.65
3 4.65
3 6.21
.. ..
120 4.65
120 3.66
120 8.55

Where the leftmost column is the second that the data printed out and the rightmost column contains the value. What I would like to do is to for each second, find the average of the values for that second and print out the result, then move on to the next second.

I only started using AWK a day or so again and have been trying to get my head around this. The idea behind the script below is, the sum will be calculated until no more data exists for that second, at which point the average is calculated and printed. The script will then move on to calculate the values for the next second. I have tried everything, but cannot see to get it to work.

Any help would be very much appreciated. Smilie

Patrick.

Code:
AWK 
BEGIN{}
{

for (i=1; i<=120; i++){

if($1 = i){
sum+=$2
count++
       }
if($1 != i){
avg = sum/count
print (i " " avg)
avg = 0
sum = 0
count = 0
break
       }
                    }

}
END{ }

# 2  
Old 08-12-2009
Almost there ...

Code:
awk '$1!=s && c { print s, sum/c; sum=c=0 } 
                { s=$1;  c++; sum+=$2  }
       END { print s, sum/c }'    filename

Output:

Code:
1 3.51
2 7.17667
3 5.43
120 5.62

Use printf for pretty printing.
# 3  
Old 08-13-2009
Rubin, works perfectly. I really appreciate your help.

Thanks.
Patrick.
# 4  
Old 08-13-2009
Another easier way is to use the associative arrays:

Code:
code:-

awk '
{a[$1]+=$2 ; b[$1]++}
END {for (i in a) {print i,a[i]/b[i]}}
' input_file | sort -n +0

Best Regards
# 5  
Old 08-14-2009
Code:
awk '{arr[$1]++;brr[$1]+=$2}END{for (i in brr)print i" "brr[i]/arr[i]}' yourfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple awk search problem

Hello; we have : awk '/reg_exp/,0/ prints every line after the first occurrence of "reg_exp" But if I want to print rest of the lines AFTER the last occurrence of "reg_exp", how would I do it ?? Tried : awk ' ! (/reg_exp/,0)' But it errored... Thank you for any... (5 Replies)
Discussion started by: delphys
5 Replies

2. Shell Programming and Scripting

simple awk sort problem

Hello folks I have the following output UNIX95=1 ps -ef -o pcpu,user,pid,args |more %CPU USER PID COMMAND 0.03 root 0 swapper 0.08 root 1 init 0.00 root 13 net_str_cached 0.00 root 12 usbhubd 0.00 root 11 escsid 0.00 root 10... (3 Replies)
Discussion started by: delphys
3 Replies

3. Shell Programming and Scripting

another simple awk problem

Hello; I need to print two previous lines after searching for a reg exp: awk '/haywood/' should produce the following =================== p9J46THe020804 89922 Tue Oct 18 21:06 MAILER-DAEMON (host map: lookup (haywood.com): deferred) ... (1 Reply)
Discussion started by: delphys
1 Replies

4. Shell Programming and Scripting

Simple awk problem II

Hello; Trying to figure out how to keep just the contents between the two search lines: awk '/regexp_1/ ,/regexp_2/' I do not want lines containing regexp_1 and regexp_2 in the output. Thank you for any ideas Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: delphys
5 Replies

5. Shell Programming and Scripting

simple awk problem

Hello; I have the following log file: 10/11/11 10:42:02 LOCK Q Userid:284 Username=root UserPID:23158 Device:marlin batch 10/11/11 10:42:02 TableNr:226 TableName:iatkn RecId:116290398 Flags:X Q H 10/11/11 10:42:02 LOCK CONTENTION X 10/11/11 10:42:02 ... (3 Replies)
Discussion started by: delphys
3 Replies

6. Shell Programming and Scripting

Simple bash script problem

#!/bin/bash cd /media/disk-2 Running ./run.sh it's not changing directory.Why? (6 Replies)
Discussion started by: cola
6 Replies

7. Shell Programming and Scripting

simple awk problem

pcn linus> ntpq -p remote refid st t when poll reach delay offset disp ============================================================================== +smpnn01 ntpsrv1 2 u 829 1024 377 1.46 0.793 0.85 *smpnn02 ntpsrv1 2 u ... (2 Replies)
Discussion started by: arch12
2 Replies

8. Shell Programming and Scripting

awk gsub simple problem

Hi New to shell script and awk and need assistance on this problem. I need to use a variable to substitute a string in an external file and write the changed info to another file. At first I did not know if you could use a variable as the sub value but the following showed me that I can. ... (3 Replies)
Discussion started by: hukcjv
3 Replies

9. Shell Programming and Scripting

Simple script problem

Hi everyone - I am sure this is a really simple problem but I'm a total noob at Linux scripting: I wanted to create a script that allows me to compare the current week number to the contents of a text file in my home directory: VAR1='date +%V' VAR2='cat /home/fred/file.txt' ... (6 Replies)
Discussion started by: FiniteRed
6 Replies

10. UNIX for Dummies Questions & Answers

simple shell script problem

hi all. i have a little problem. im basically reading input from the user from the keyboard into the variable "phonenumber". I want to do a little error check to check if the user doesnt enter anything in for the value phonenumber. i had this: read phonenumber if then ..... else ........ (2 Replies)
Discussion started by: djt0506
2 Replies
Login or Register to Ask a Question