Topic: Counting unique lines from a parsed text file

Hey there, forums.

My current predicament is that I am parsing a log file for a website to gather information for an admin page. The log file contains the time, userid, and session-token for each login the website completes. My goal is to end up with an integer that represents the number of unique users who logged into the site on a given day. Any ideas?

Just for some additional information, we open the text file and then parse it using a regex, putting the 3 pieces of information into an array. Hope that's enough information to at least get some questions going. Thanks in advance, everyone.

dacheat

Re: Counting unique lines from a parsed text file

Hi dacheat,

Can you post the format of your log file/array (perhaps the output of array.inspect)? The cleanest solution would be to generate an array of hashes, then you could do something like:

logins.collect{|login| login[:user]}.uniq.length

Alex

Re: Counting unique lines from a parsed text file

alexpt wrote:

Hi dacheat,

Can you post the format of your log file/array (perhaps the output of array.inspect)? The cleanest solution would be to generate an array of hashes, then you could do something like:

logins.collect{|login| login[:user]}.uniq.length

Alex

The log file has lines that look basically like this:
Login: <[Date/Time]> UserID: [userid as int] Token: [session token as string]

The regex pulls everything the text i put between [ ] and stores them in an array a.
the array would look like:
a[0] = date.time
a[1] = userid as int
a[2] = session token


I'm at work so I can't get to my code right now. Could you give me a quick rundown of how an array of hashes works? I'm pretty new to ruby so the syntax/structures are still pretty foreign. Thanks so much.

dacheat

Re: Counting unique lines from a parsed text file

You'd do something like this:

logins = File.open("blah.log").readlines.collect do |l| 
  vars = l.match /\[([^\]*)]/ # put your regexp here
  return {:date => vars[0], :id => vars[1], :session_token => vars[2]}
end

number_of_unique_users = logins.collect{|login| login[:id]}.uniq.length


Alex

Last edited by alexpt (2007-06-26 15:48:22)

Re: Counting unique lines from a parsed text file

alexpt wrote:

You'd do something like this:

logins = File.open("blah.log").readlines.collect do |l| 
  vars = l.match /\[([^\]*)]/ # put your regexp here
  return {:date => vars[0], :id => vars[1], :session_token => vars[2]}
end

number_of_unique_users = logins.collect{|login| login[:id]}.uniq.length


Alex

Thanks for the help! That looks like it will do exactly what I need. I will try this tonight when I get home. Have a good one.

dacheat

Re: Counting unique lines from a parsed text file

There is a slight mistake with the regular expression, it should have read /\[([^\]]*)/

Alex wink