i have written a simple code for removing the duplicates from a csv file. but now i want to modify this code and want to check if duplicates are there in certain fields. ie am having fields eg-file_name,id,user,group,permissions,links,path,date,file_size etc in a csv and i want file_name,id,user,date,file_size fields to be considered to check duplicates how do i go about. and i also want to output the duplicate record to another file.
#!/usr/bin/perl -w use strict; # Set to filename of CSV file my $csvfile = ''; # Set to filename of new file(file without duplicates) my $newfile = ''; # Set to 1 if first line of CSV file contains field names, 0 otherwise my $fieldnames = 1; ### Shouldn't need to change stuff below here ### open (IN, "<$csvfile") or die "Couldn't open input CSV file: $!"; open (OUT, ">$newfile") or die "Couldn't open output file: $!"; # Read header lines if they exist my $header; $header = <IN> if $fieldnames; # Slurp in & sort everything else my @data = sort <IN>; # If we read in a header line, throw it back out again print OUT $header; my $n = 0; # Now go through the data line by line, writing it to output unless # to the previous line (in which case it's a dupe) my $lastline = ''; foreach my $currentline (@data) { next if $currentline eq $lastline; print OUT $currentline; $lastline = $currentline; $n++; } close IN; close OUT; print "Processing complete. In = " . scalar @data . " records, Out = $n records\n";

In reply to modifying the remove duplicates code by aish

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.