Solved

Perl sort descending by 2nd column and print/write the first line

Posted on 2009-05-05
3
828 Views
Last Modified: 2012-06-21
Hey guys,

I have a file (PFA) and I will need to do in perl the following:

for each 'YYYY-MM-DD HH NN' to sort descending by second column then print/write just the first (highest) value like: YYYY-MM-DD HH:00 NN. Please bear in mind while sorting, I want that the first column to be taken without minutes (MM). Should be a king of group by YYYY-MM-DD HH then get the highest value of second column. I need this for different day/hour/etc. The columns are tab separated, like YYYY-MM-DD HH:MM \t NN.

 As example, the results of below lines should be:

2009-03-04 15:00      34

2009-03-04 15:28      30
2009-03-04 15:29      30
2009-03-04 15:30      28
2009-03-04 15:31      30
2009-03-04 15:32      30
2009-03-04 15:33      31
2009-03-04 15:34      32
2009-03-04 15:35      31
2009-03-04 15:36      34
2009-03-04 15:37      34
2009-03-04 15:38      32
2009-03-04 15:39      31
2009-03-04 15:40      30
2009-03-04 15:41      29
2009-03-04 15:42      28


I hope everything is clear. If not, please let me know. Thanks a lot in advance.

Cheers,
Dan.
mylog.txt
0
Comment
Question by:dzamfir
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24305396
Pass it the name of your log file on the command line, like:
    script.pl mylog.txt
It will write to stdout.  To save to a file, use like:
    script.pl mylog.txt > output.txt

my %vals;

while(<>) {

	next unless /(\d{4}-\d{2}-\d{2} \d{2}):\d{2}\s+(\d+)/;        #\s+

	my ($date, $number) = ($1, $2);

	$vals{$date} = $2 if(!exists($vals{$date}) or $vals{$date}<$number);

}
 

foreach (sort {($a cmp $b) or ($vals{$a} <=> $vals{$b})} keys %vals) {

	print "$_:00\t$vals{$_}\n";

}

Open in new window

0
 
LVL 6

Author Comment

by:dzamfir
ID: 24306007
Seems to be fine. I will check tomorrow and then I will give you a feedback.

Thanks.
0
 
LVL 6

Author Comment

by:dzamfir
ID: 24356327
Adam,

Sorry for delay. Your solution is great. Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

19 Experts available now in Live!

Get 1:1 Help Now