finding duplicates with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding duplicates with perl
# 1  
Old 01-27-2003
finding duplicates with perl

I have a huge file (over 30mb) that I am processing through with perl. I am pulling out a list of filenames and placing it in an array called @reports.
I am fine up till here. What I then want to do is go through the array and find any duplicates. If there is a duplicate, output it to the screen. But once I find one duplicate of a filename, I want to go through and look for a duplicate of the next filename.
Thanks!
# 2  
Old 01-27-2003
Without more specifics about your problem, I think a hash might be more appropriate than an array. Then you can keep a count of where each filename is called out, or a list of callouts or whatever. If you need to preserve the order of the filenames, store the record number each filename was first found in, say, then sort on the record number. But a hash is a fundamental perl idiom for detecting duplicates. It'll work in ruby, too, BTW.
# 3  
Old 01-27-2003
I'm not really sure what exactly that would entail. I will post what I have thought of (using an array), although it is not working.


open(DUPS, ">duplicates.txt") or die "Can't open duplicates.txt $!";
for $i (0 .. $#reports){
for $j (0 .. $#reports){
if ($i != $j && $reports[$i] eq $reports[$j]){
print DUPS "\n $reports[$i]";
last;
}

}

}
close(DUPS);
# 4  
Old 01-28-2003
Okay, if you need the list of @reports in an array for some other reason, the next best thing is a hash just to gather the duplicates. You can destroy the hash later if you need to. Warning: untested code
Code:
%h = ();
foreach $r (@reports) {
    if (!exists($h{$r}))  {
        # First time we've seen this one
        $h{$r} = 0
    } elsif ($h{$r}) {
        # We've seen this one before and reported
        $h{$r}++
    } else {
        # Second time, so report the duplicate
        print DUPS "\n $r";
        $h{$r} = 1
    }
}
%h = ();   # Destroy %h if you're done with it

Now %h contains the number of "extra" of each member of @reports, i.e, one less than the number that's actually there. If you don't need @reports for anything else, you can embed this logic into the loop thats reading your large data file and save some memory.

Another option is to put off the reporting of duplicates, putting that in another loop after the one shown above so you can report the number of times a report is found in @reports.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in modifying a PERL script to sort Singletons and Duplicates

I have a large database which has the following structure a=b where a is one language and b is the other and = is the delimiter Since the data treats of language, homographs occur i.e. the same word on the left hand side can map in two different entries to two different glosses on the right... (3 Replies)
Discussion started by: gimley
3 Replies

2. Shell Programming and Scripting

UNIX scripting for finding duplicates and null records in pk columns

Hi, I have a requirement.for eg: i have a text file with pipe symbol as delimiter(|) with 4 columns a,b,c,d. Here a and b are primary key columns.. i want to process that file to find the duplicates and null values are in primary key columns(a,b) . I want to write the unique records in which... (5 Replies)
Discussion started by: praveenraj.1991
5 Replies

3. Shell Programming and Scripting

PERL "filtering the log file removing the duplicates

Hi folks, I have a log file in the below format and trying to get the output of the unique ones based on mnemonic IN PERL. Could any one please let me know with the code and the logic ? Severity Mnemonic Log Message 7 CLI_SCHEDULER Logfile for scheduled CLI... (3 Replies)
Discussion started by: scriptscript
3 Replies

4. Shell Programming and Scripting

Finding duplicates in a file excluding specific pattern

I have unix file like below >newuser newuser <hello hello newone I want to find the unique values in the file(excluding <,>),so that the out put should be >newuser <hello newone can any body tell me what is command to get this new file. (7 Replies)
Discussion started by: shiva2985
7 Replies

5. Shell Programming and Scripting

Perl, sorting and eliminating duplicates

Hi guys! I'm trying to eliminate some duplicates from a file but I'm like this :wall: !!! My file looks like this: ID_1 0.02 ID_2 2.4e-2 ID_2 4.3.e-9 ID_3 0.003 ID_4 0.2 ID_4 0.05 ID_5 1.2e-3 What I need is to eliminate all the duplicates considering the first column (in this... (6 Replies)
Discussion started by: gabrysfe
6 Replies

6. UNIX for Dummies Questions & Answers

Finding duplicates then copying, almost there, maybe?

Hi everyone. I'm trying to help my wife with a project, she has exported 200 images from many different folders, unfortunately there was a problem with the export and I need to find the master versions so that she doesn't have to go through and select them again. I need to: For each image in... (2 Replies)
Discussion started by: Rhinoskin
2 Replies

7. Shell Programming and Scripting

finding duplicates in csv based on key columns

Hi team, I have 20 columns csv files. i want to find the duplicates in that file based on the column1 column10 column4 column6 coulnn8 coulunm2 . if those columns have same values . then it should be a duplicate record. can one help me on finding the duplicates, Thanks in advance. ... (2 Replies)
Discussion started by: baskivs
2 Replies

8. Shell Programming and Scripting

Help finding non duplicates

I am currently creating a script to find filenames that are listed once in an input file (find non duplicates). I then want to report those single files in another file. Here is the function that I have so far: function dups_filenames { file2="" file1="" file="" dn="" ch="" pn="" ... (6 Replies)
Discussion started by: chipblah84
6 Replies

9. Shell Programming and Scripting

Finding duplicates from positioned substring across lines

I have million's of records each containing exactly 50 characters and have to check the uniqueness of 4 character substring of 50 character (postion known prior) and report if any duplicates are found. Eg. data... AAAA00000000000000XXXX0000 0000000000... upto50 chars... (2 Replies)
Discussion started by: gapprasath
2 Replies

10. Shell Programming and Scripting

finding duplicates in columns and removing lines

I am trying to figure out how to scan a file like so: 1 ralphs office","555-555-5555","ralph@mail.com","www.ralph.com 2 margies office","555-555-5555","ralph@mail.com","www.ralph.com 3 kims office","555-555-5555","kims@mail.com","www.ralph.com 4 tims... (17 Replies)
Discussion started by: totus
17 Replies
Login or Register to Ask a Question