LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2015, 06:07 AM   #1
Newman1
LQ Newbie
 
Registered: Feb 2015
Posts: 10

Rep: Reputation: Disabled
Calculate Average time of a column


Hello dears,

I have a log file with records like below and want to get a average of one column based on the search of one specific keyword.

2015-02-07 08:15:28 10.102.51.100 10.112.55.101 "kevin.c" POST /mainportal/webflow/customerprofile/smsEmailNotications/loadSubCategories.do?&msisdn=374355752&categoryCode=GSM_19 0.391 200 1740

suppose keyword is "loadSubCategories" and response time is on "column 8" (0.391)

This command gives a average of all response time of column 8 but not with the given keyword.

awk '{ total += $8; count++ } END { print total/count }' file.log001

please let me know how to write it.

Thanks.

Last edited by Newman1; 02-07-2015 at 07:22 AM.
 
Old 02-07-2015, 06:26 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> This command gives a average of all response time of column 8 but not with the given keyword.

Which command did you mean?
 
Old 02-07-2015, 06:31 AM   #3
Newman1
LQ Newbie
 
Registered: Feb 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Sorry,post edited...
 
Old 02-07-2015, 07:34 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
I presume $6 has the key word.

So change it to
awk '{ if($6=="loadSubcategories") {total += $8; count++} } END { print total/count }' file.log001

OK
 
Old 02-07-2015, 07:53 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,636
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
Or ... and I'm serious ... just slurp it into a spreadsheet and ask for sum(). Solved.
 
Old 02-07-2015, 10:04 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... not sure how AnanthaP's solution would work as the sixth field is not equal to that string??

However, the logic is similar to what you already have, just tell awk to only add up the eighth column when your line contains the string required, ie. look up the use of //
 
1 members found this post helpful.
Old 02-07-2015, 10:50 AM   #7
Newman1
LQ Newbie
 
Registered: Feb 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
It doesn't work with == as the keyword is a part of the string!
how to replace the the equal sign with like command?
 
Old 02-08-2015, 11:02 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
awk '{ if (index ($6, "loadSubcategories")) {total += $8; count++} } END { print total/count }' file.log001
 
1 members found this post helpful.
Old 02-12-2015, 12:52 PM   #9
Newman1
LQ Newbie
 
Registered: Feb 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank You everybody for your help.

Below script also seems works correctly...

awk '/loadSubcategories/ {total += $8; count++} END { print total/count }' access.log00001
 
Old 02-12-2015, 12:56 PM   #10
Newman1
LQ Newbie
 
Registered: Feb 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Code:
awk '{ if (index ($6, "loadSubcategories")) {total += $8; count++} } END { print total/count }' file.log001
Dear NevemTeve,thanks it works.
 
  


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
How to calculate the average delay of the packets? sowpra Linux - Software 3 04-18-2012 01:57 AM
Average of a column in console output santius Linux - General 4 09-03-2010 11:50 PM
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 > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 AM.

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