You could try something like this:

#!/usr/bin/perl use warnings; use strict; my %uniq; while (<DATA>) { next unless /^\s*\d/; chomp; my $line = $_; my @f = split /,/, $line; my $key = $f[1].$f[2].$f[3]; if ( exists $uniq{$key} ) { my $stored = ( split /,/, $uniq{$key})[4]; my $new = $f[4]; if ($new lt $stored) { $uniq{$key} = $line; } } else { $uniq{$key} = $line; } } print $_."\n" for (values %uniq); __DATA__ 1,ken,james,smith,s 11,ken,james,smith,f 0,ken,james,smith,s 5,ken,arthur,wesson,g 7,ken,arthur,wesson,a

For the provided DATA section, it produces the following output:

11,ken,james,smith,f 7,ken,arthur,wesson,a

Which should be the behavior you want.

Consider looking at dedicated CSV modules, like Text::CSV_XS.

It's already 2015 in my time zone, and so I wish you all the best in 2015. May your code produce the output you desire, and your input be as you think it is.

- Luke


In reply to Re: perl to remove duplacate based on columnb,d & by blindluke
in thread perl to remove duplacate based on columnb,d & by john.tm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.