Solved

Perl - formate date and count

Posted on 2013-01-31
2
306 Views
Last Modified: 2013-01-31
I have some very long text files with lines such as:

1st column is the date:  2nd column is an id number

20120101: 549017
20120101: 339146
20120101: 535768
20120102: 535768
20120102: 541576
20120102: 292492
20120201: 442169
20120201: 438927
20120201: 574126
20120201: 592525
20120301: 361568
20120301: 535768
20120301: 535768
20120401: 535768
20120401: 535768

I'm trying to get a count of how many times each ID number was used per month, so can take the first 6 digits of the date to get the year/month and then match the ID's for each month with a count.

I'm getting part of what I need but not sure how to group the months in this way then do a count per ID with each month.

Need to get an output in a format such as:

ID             Month-Year  Times-Used
535768     Jan-2012       1322
695232     Feb-2012       3214
.......

Thanks for any help suggestions.
0
Comment
Question by:bt707
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 38842103
#!/usr/bin/perl
my %count;
/\b(\d{6})\d\d:\s*(\d+)/ && $count{$2}{$1}++ while <DATA>;
print "ID             Month-Year  Times-Used\n";
for my $id (sort keys %count ){
   for $mon (sort keys %{$count{$id}} ){
       printf"%-14d %s-%.4s    %d\n",$id,(qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon%100-1],$mon,$count{$id}{$mon};
    }
}


__DATA__
20120101: 549017
    20120101: 339146
    20120101: 535768
    20120102: 535768
    20120102: 541576
    20120102: 292492
    20120201: 442169
    20120201: 438927
    20120201: 574126
    20120201: 592525
    20120301: 361568
    20120301: 535768
    20120301: 535768
    20120401: 535768
    20120401: 535768
0
 

Author Closing Comment

by:bt707
ID: 38842117
Thanks ozo, way better than what all I was doing.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question