LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-05-2015, 11:05 AM   #1
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Rep: Reputation: 1
Calculate average of column in units of time (HH:MM)


Here is an example of my data.

Code:
Group1,2015-04-21,05:05,PASS
Group1,2015-04-22,10:16,PASS
Group1,2015-04-23,05:25,PASS
Group1,2015-04-24,06:38,PASS
Group1,2015-04-25,06:27,PASS
Group1,2015-04-26,10:11,PASS
Group1,2015-04-27,05:48,PASS
Group1,2015-04-28,00:03,FAIL
Group1,2015-04-29,05:33,PASS
Group1,2015-04-30,05:30,PASS
Group1,2015-05-01,13:39,PASS
Group1,2015-05-02,06:53,PASS
Group1,2015-05-03,09:51,PASS
Group1,2015-05-04,06:01,PASS
What I want is an average of column 3 which should be easy, right?

Code:
awk -F',' '{ total += $3; count++ } END { print total/count }' test.txt
6.5
6.5 is not correct and is in decimal form. According to excel (yes, I am lazy) It should be 6 hours, 57 Minutes, 9 seconds. What do I need to do differently in order to get the correct answer in the format HH:MM?
 
Old 05-05-2015, 11:27 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,308

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this mite help:
http://www.unix.com/unix-for-dummies...rage-time.html
 
Old 05-05-2015, 11:33 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,192

Rep: Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829Reputation: 6829
as excel will recognize date format and calculate average based on that you need to do the same thing. Convert hours:minutes into a usable format and calculate average. But I think you need to take into account the date too.
you may find this page useful to do the conversion: http://www.theunixschool.com/2013/01...functions.html
 
Old 05-05-2015, 11:36 AM   #4
Samsonite2010
Member
 
Registered: Apr 2015
Distribution: Debian
Posts: 267
Blog Entries: 1

Rep: Reputation: 117Reputation: 117
If it really is just the hours and minutes you are interested in you probably want to multiply the hours by 60 then add the minutes then you will be dealing with one unit (minutes). As pan64 suggests - they date might be important?
 
Old 05-05-2015, 11:42 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,308

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
[schneidz@hyper density]$ date +s -d '2015-04-21 05:05'
1429607100
[schneidz@hyper density]$ date -d @1429607100
Tue Apr 21 05:05:00 EDT 2015
[schneidz@hyper density]$ man date
 
Old 05-05-2015, 07:56 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,822

Rep: Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003
The OP apparently is happy to ignore the date (based on excel example).
Using [,:] as FS busts the time up - the arithmetic becomes simple. If there is a lot of data, I'd be looking to avoid call-outs to date if I could.
 
Old 05-06-2015, 05:38 PM   #7
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Original Poster
Rep: Reputation: 1
Correct. There is a lot of data. This is a tiny snip from a much larger group of files. The date is not important in this scenario, just part of the report that I have to maneuver around to get what I need which is averages for the hours and minutes it took to run. It looks like I am just failing maths

Last edited by thealmightyos; 05-06-2015 at 06:22 PM. Reason: duh momment
 
Old 05-06-2015, 08:15 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,822

Rep: Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003Reputation: 4003
I like to use things like this to expose the data - makes it easy to see what fields you need to use. The hours is one field, the minutes the next. As suggested above, totalling them should be trivial. In your END clause do the final division - or (better) use modulo 60.
Code:
awk -F"[,:]"  'NR < 3 {print "\nRecord: " NR ; for (i=1; i<=NF; i++) print "Field "i":\t",$i}' file
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate Average time of a column Newman1 Programming 9 02-12-2015 12:56 PM
How to calculate the average delay of the packets? sowpra Linux - Software 3 04-18-2012 01:57 AM
calculate the average of cells in columns in separate txt files Mike_V Programming 16 05-16-2009 04:37 AM
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 12:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration