Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-05
3
Medium Priority
?
889 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

636 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