Solved

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

Posted on 2009-05-05
3
874 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

688 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