LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Calculate Average time of a column (https://www.linuxquestions.org/questions/programming-9/calculate-average-time-of-a-column-4175533333/)

Newman1 02-07-2015 06:07 AM

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.

NevemTeve 02-07-2015 06:26 AM

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

Which command did you mean?

Newman1 02-07-2015 06:31 AM

Sorry,post edited...

AnanthaP 02-07-2015 07:34 AM

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

sundialsvcs 02-07-2015 07:53 AM

Or ... and I'm serious ... just slurp it into a spreadsheet and ask for sum(). Solved. :)

grail 02-07-2015 10:04 AM

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 //

Newman1 02-07-2015 10:50 AM

It doesn't work with == as the keyword is a part of the string!
how to replace the the equal sign with like command?

NevemTeve 02-08-2015 11:02 AM

Code:

awk '{ if (index ($6, "loadSubcategories")) {total += $8; count++} } END { print total/count }' file.log001

Newman1 02-12-2015 12:52 PM

Thank You everybody for your help.

Below script also seems works correctly...

awk '/loadSubcategories/ {total += $8; count++} END { print total/count }' access.log00001

Newman1 02-12-2015 12:56 PM

Quote:

Originally Posted by NevemTeve (Post 5313981)
Code:

awk '{ if (index ($6, "loadSubcategories")) {total += $8; count++} } END { print total/count }' file.log001

Dear NevemTeve,thanks it works.


All times are GMT -5. The time now is 05:00 PM.