Solved

Pick the row for each day with highest value in the last column then delete the rest using perl

Posted on 2011-09-22
3
227 Views
Last Modified: 2012-05-12
I have a text file in this format. Below. I have the perl script or shell script to simply locate the highest row for each day, keep that line for each day then delete the rest. Some days may be missing. The output file will look just like the input file except smaller with one row per day. INput file may have hundreds of days worth of data. Output file would ideally have the final value in the last column round to the nearest hundredth i.e. 0.00 but it is not critical.

inputfile.txt
11-21-10 00:00:00 0.033
11-21-10 00:00:00 0.0146666666666667
11-21-10 00:00:00 0.00366666666666667
11-21-10 00:00:00 0.000333333333333333
11-22-10 00:00:00 0.00466666666666667
11-22-10 00:00:00 0.031
11-22-10 00:00:00 0.0276666666666667
11-22-10 00:00:00 0.005
11-22-10 00:00:00 0.00133333333333333
11-22-10 00:00:00 0
11-22-10 00:00:00 0
11-23-10 00:00:00 0
11-23-10 00:00:00 0
11-23-10 00:00:00 0
11-23-10 00:00:00 0.000666666666666667
11-23-10 00:00:00 0
11-23-10 00:00:00 0
11-23-10 00:00:00 0
11-24-10 00:00:00 0
11-24-10 00:00:00 0
11-24-10 00:00:00 0
11-24-10 00:00:00 0
11-24-10 00:00:00 0
11-24-10 00:00:00 0
11-24-10 00:00:00 0
11-25-10 00:00:00 0

output file.txt
11-21-10 00:00:00 0.033
11-22-10 00:00:00 0.031
11-23-10 00:00:00 0.000666666666666667
11-24-10 00:00:00 0
11-25-10 00:00:00 0
vcnow.txt
0
Comment
Question by:libertyforall2
3 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 250 total points
ID: 36584489
Try this for size:

perl -ane'$k=join(" ",@F[0,1]); $s{$k}=$F[2] if $F[2]>$s{$k}; }{print map{"$_ $s{$_}\n"} sort keys %s' vcnow.txt > output_file
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
ID: 36586979
This should do what you want.
#!/usr/local/bin/perl

use strict;
use warnings;

# change these values if necessary
my $infile = 'inputfile.txt';
my $outfile = 'outputfile.txt';

open IN, $infile or die "could not open $infile: $!";
my %max;
while (<IN>) {
    chomp;
    my ($dt, $tm, $val) = split;
    if (not exists $max{$dt} or $val > $max{$dt}[0]) {
        $max{$dt} = [$val, $_];
    }
}
close IN;

open OUT, '>', $outfile or die "could not write $outfile: $!";
foreach my $dt (sort keys %max) {
    print OUT $max{$dt}[1], "\n";
}
close OUT;

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 36898443
Great!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now