INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

awk help counting number of entries per hour

awk help counting number of entries per hour

(OP)
Hi,
I'm struggling trying to put together something to grab out details from many logfiles created over a 24 hour period and count how many times per hour a name appears.
I want to run through every hour (i.e. 00,01,02.....23) and print out how many times name 'x' was found.

The only fields I'm interested in are;
The hour is in field 2
The name is in field 11


My input file(s) are comma delimited and look like;
2013/07/13,04:54:33,333,444,555,666,777,888,999,101010,John,121212,131313,141414,151515,161616,171717,181818,191919

I would like my output to look like (NAME HOUR - No. of Entries)
John 00 - 123
Marc 00 - 101
John 01 - 96
Marc 01 - 300
etc...


I've got so far with the code but the totals per hour it prints out are the GRAND TOTAL of entries per name. So using the example above 'john' would show 219 for hours 00 AND 01 (123+96).

My code looks like;

awk -F"," '{ split($2,DTM,":");a[DTM[1]]++;count[$11]++}END{for(i in a) {for(name in count) printf "%30s - %d\n", name" " i, (count[name]) | "sort -k1" }}'

And its output looks like this;
John 00 - 219
Marc 00 - 401
John 01 - 219
Marc 01 - 401


I know its probably a simple change, but i've hit a wall and cant find the right syntax, so any help you can give would be very appreciated.
Thanks.

RE: awk help counting number of entries per hour

(OP)
Thanks for the quick reply PHV.
I neglected to say in my original post that im using Solaris

I tried running you suggestion but it hit a syntax error;

awk: syntax error near line 1
awk: illegal statement near line 1

I also tried running it through "/usr/xpg4/bin/awk" but that also hot an error;

/usr/xpg4/bin/awk: syntax error Context is:
>>> n count) printf "%30s - %d\n", name, (count[name]) <<<

Are you able to help any further with this considering its on old awk/solaris?

thanks.

RE: awk help counting number of entries per hour

(OP)
Just looked again at your post PHV and i changed it slightly to;

awk -F"," '{split($2,DTM,":");count[$11" "DTM[1]]++}END{for(name in count) printf "%30s - %d\n", name, (count[name]) | "sort -k1" }'

This works exactly how i wanted it too, so a big thankyou to you for your help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close