Average in awk based on time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Average in awk based on time
# 15  
Old 07-31-2008
FWIW, I tried Franklin52's code and it worked okay for me.

1. I made a data file called, "kathy.dat" with the contents:
Code:
site1,"2000-01-01 00:00:00", "2000-01-01 00:59:00",0.013
site2,"2000-02-01 01:00:00", "2000-02-01 01:59:00",0.035
site1,"2000-02-01 02:00:00", "2000-02-01 02:59:00",0.034
site3,"2000-03-01 03:00:00", "2000-03-01 03:59:00",0.021
site4,"2000-02-01 02:00:00", "2000-02-01 02:59:00",-999
site1,"2000-01-01 01:00:00", "2000-01-01 01:59:00",0.012
site5,"2000-05-01 03:00:00", "2000-05-01 03:59:00",0.034

2. Then I made a shell script file called, "Franklin52.sh" with the contents:
Code:
#!/bin/sh

awk -F, '
  $4>0{
    split($2,s," ")
    sub("\"","",s[1])
    e=s[1]
    a[e]+=$4
    b[e]++
  }
END{
  for(i in a){
    print "Average for " i " = " a[i]/b[i]
  }
}' kathy.dat

3. I made the shell script file executable like this:
Code:
chmod +x Franklin52.sh

4. Then I ran the script like this:
Code:
./Franklin52.sh

5. And got these results:
Code:
Average for 2000-02-01 = 0.0345
Average for 2000-03-01 = 0.021
Average for 2000-05-01 = 0.034
Average for 2000-01-01 = 0.0125

Now that's sweeet!! Smilie
# 16  
Old 08-01-2008
MySQL find the problem

Thank you so much, Cassj and Franklin52! I used csh (#!/bin/csh) before and always got error (Unmatched '). Now I switched to sh (#!/bin/sh) as Cassj did, everything is the same as before, then it worked fine. Thank you very very very much!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Average columns based on header name

Hi Friends, I have files with columns like this. This sample input below is partial. Please check below for main file link. Each file will have only two rows. ... (8 Replies)
Discussion started by: jacobs.smith
8 Replies

2. Shell Programming and Scripting

Awk: time intervals based on epoch time

I have a list of epoch times delimited by "-" as follows: 1335078000 - 1335176700 1335340800 - 1335527400 1335771300 - 1335945600 1336201200 - 1336218000 The corresponding dates are: 20120422 1000 - 20120423 1325 20120425 1100 - 20120427 1450 20120430 1035 - 20120502 1100 ... (3 Replies)
Discussion started by: alex2005
3 Replies

3. Shell Programming and Scripting

KSH or AWK for fetching the first and the last line based on time.

Dears could anybody please help me with the below task as I am a newbie to programming. I have a text file with 4 columns (priv_ip,time,pub_ip). eg: 10.160.101.160,0708044510,22.203.195.151 10.160.101.160,0708044645,22.203.195.151 10.160.101.160,0708050410,22.203.195.151... (5 Replies)
Discussion started by: BrownBob
5 Replies

4. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

AWK: how to get average based on certain column

Hi, I'm new to shell programming, can anyone help me on this? I want to do following operations - 1. Average salary for each country 2. Total salary for each city and data that looks like - salary country city 10000 zzz BN 25000 zzz BN 30000 zzz BN 10000 yyy ZN 15000 yyy ZN ... (3 Replies)
Discussion started by: shell123
3 Replies

6. Shell Programming and Scripting

Average calculation based on number of rows

Dear users, I need your support, I have a file like this: 272134.548 6680572.715 272134.545 6680572.711 272134.546 6680572.713 272134.548 6680572.706 272134.545 6680572.721 272134.543 6680572.710 272134.544 6680572.715 272134.543 6680572.705 272134.540 6680572.720 272134.544... (10 Replies)
Discussion started by: Gery
10 Replies

7. Shell Programming and Scripting

calculate the average of time series data using AWK

Hi, I have two time series data (below) merged into a file. t1 and t2 are in unit of second I want to calculate the average of V1 every second and count how many times "1" in V2 is occur within a second Input File: t1 V1 t2 V2 10.000000... (5 Replies)
Discussion started by: nica
5 Replies

8. Shell Programming and Scripting

Remove lines, Sorted with Time based columns using AWK & SORT

Hi having a file as follows MediaErr.log 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:12:16 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:22:47 84 Server1 Policy1 Schedule1 master1 05/08/2008 03:41:26 84 Server1 Policy1 ... (1 Reply)
Discussion started by: karthikn7974
1 Replies

9. UNIX for Dummies Questions & Answers

Average Time

Hi Guys, I am using a command $ runprt_req PPGUS_ROYXN1102 Output: **************** Start Date and Time End Date and Time*** PPGUS_ROYXN1102 01/15/2008 02:20:08 01/15/2008 04:54:50 PPGUS_ROYXN1102 01/12/2008 02:03:57 01/12/2008 04:22:10... (1 Reply)
Discussion started by: sambond
1 Replies

10. Shell Programming and Scripting

average transaction time

Hi all, I have large daily log file(s) that hold the times for requests and responses on different system requests. What I want to do is work out average transaction times for the day (one log = one day). The problem I'm having is figuring out how to skip rows, i've sorted the output by uniq... (2 Replies)
Discussion started by: nhatch
2 Replies
Login or Register to Ask a Question