dzamfir
asked on
Perl sort descending by 2nd column and print/write the first line
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adam,
Sorry for delay. Your solution is great. Thank you.
Sorry for delay. Your solution is great. Thank you.
ASKER
Thanks.