Solved

Perl - formate date and count

Posted on 2013-01-31
2
303 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now