?
Solved

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

Posted on 2009-05-05
3
Medium Priority
?
882 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to count occurrences of each item in an array.
Six Sigma Control Plans
Suggested Courses

770 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