The following solution isn't as short as the other solutions, but it will
allow you to maintain the order of the original file:

#!/usr/bin/perl
use warnings;
use strict;

my($last, $last_rec);

while (<DATA>) {
  my @data = split;
  if ($last and $data[0] != $last) {
    print $last_rec;
  } elsif (eof) {
    print;
  }
  $last = $data[0];
  $last_rec = $_;
}

__DATA__
1  5  4
1  0  2
1  2  2
2  0  2
3  0  3
3  4  6


"Loan Tran" <[EMAIL PROTECTED]> wrote in message...

> I would like to know if there is any way to process a
> file using perl to get rid of extra records based on a
> key value.
> My key is the 1st field and in my output file I only
> want 1 record per key and it has to be the last record
> in the file.
>
> So for example here is my file:
> 1  5  4
> 1  0  2
> 1  2  2
> 2  0  2
> 3  0  3
> 3  4  6
> And here is the output file I want:
> 1  2  2
> 2  0  2
> 3  4  6



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to