Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS CLI issues with Tags 3 77
Facebook React and SyntheticEvents 6 67
Is it possible to create a playfile with setacl or icacle? 3 42
How can i open a .fxp file 3 33
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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