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";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.