Help in modifying a PERL script to sort Singletons and Duplicates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in modifying a PERL script to sort Singletons and Duplicates
# 1  
Old 07-03-2015
Help in modifying a PERL script to sort Singletons and Duplicates

I have a large database which has the following structure
Code:
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 hand side as in th example below.
Code:
a=b
a=c
b=p
b=q
c=d
r=s

I had written the following script which does give results when the database is small but when the database increases (around 100,000+ entries) it does not perform very well. The expected output in a single file would be as under:
Code:
SINGLETONS
c=d
r=s
DUPES
a=b
a=c
b=p
b=q

I am giving the Perl script below
Code:
#!/usr/bin/perl

$dupes = $singletons = "";		# This goes at the head of the file

do {
    $dupefound = 0;			# These go at the head of the loop
    $text = $line = $prevline = $name = $prevname = "";
    do {
	$line = <>;
	$line =~ /^(.+)\=.+$/ and $name = $1;
	$prevline =~ /^(.+)\=.+$/ and $prevname = $1;
	if ($name eq $prevname) { $dupefound += 1 }
	$text .= $line;
	$prevline = $line;
    } until ($dupefound > 0 and $text !~ /^(.+?)\=.*?\n(?:\1=.*?\n)+\z/m) or eof;
    if ($text =~ s/(^(.+?)\=.*?\n(?:\2=.*?\n)+)//m) { $dupes .= $1 }
    $singletons .= $text;
} until eof;
print "SINGLETONS\n$singletons\n\DUPES\n$dupes";

I feel that the problem lies with a prior sort of the data since the script gives better results (but not perfect) when sorted.
Could someone please provide a better solution and if the code is being modified, could the change be commented since I am still learning the intricacies of Perl, which explains the comments which I added on to the script.
Many thanks for your help
# 2  
Old 07-03-2015
It doesn't perform very well because you're keeping 100,000 things in one string and using regular expressions to find them. This is never going to be efficient -- you're scanning through all 100,000 things every time you add a single one.

Use a hash to organize it into several smaller things instead of one big thing of 100,000 and avoid needing regexes to find them.

Code:
#!/usr/bin/perl

my %hg=(), $n;
my @s=();

do {
        my ($a, $b, $c)=split(/[=\n]/,<STDIN>);

        if(!defined($hg{$a})) { $hg{$a}="";     }

        # Append string to hash{a}
        $hg{$a}=$hg{$a} . " " . $b;
} until eof;

foreach my $a (keys %hg) {
        my @l=split(/ /, $hg{$a});

        # save singletons for later
        if($#l == 1) {  push(@s, $a);   }
        else    {
                for($n=1; $n<=$#l; $n++)
                {
                        printf("%s=%s\n", $a, $l[$n]);
                }
        }
}

print "SINGLETONS\n";

for($n=1; $n <= $#s; $n++)
{
        printf("%s=%s\n", $s[$n], $hg{$s[$n]});
}

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-03-2015
Use as starting point.

Code:
#!/usr/bin/perl

# segregate.pl

use strict;
use warnings;

# data structure as a hash
my %db;
# populate a data structure based on first field
while(my $line = <>) {
    # remove new line
    chomp $line;
    # get field one
    my ($id) = split /=/, $line;
    # add field1 => line to the structure
    push @{$db{$id}}, $line;
}

# sorted keys for singletons
my @single;
# sorted keys for duplicates
my @dup;
# iterate through db hash using sorted keys
for my $key (sort keys %db) {
    if(scalar @{$db{$key}} == 1) {
        # add to singles list if one item
        push @single, $key;
    }
    else {
        # more than one item, add to dup list
        push @dup, $key;
    }
}

# Display lists
print "SINGLETONS:\n";
# iterate only through sorted singles
for my $single (@single) {
    print @{$db{$single}}, "\n";
}

print "DUPES:\n";
# iterate only through sorted duplicates
for my $dup (@dup) {
    print join "\n", @{$db{$dup}};
    print "\n";
}

Usage:
perl segregate.pl datafile
This User Gave Thanks to Aia For This Post:
# 4  
Old 07-03-2015
Many thanks for your help and above all for taking the pains to comment the code. Walking through it helped me understand what was wrong with my approach. The script worked and was fast and neatly segregated the two types.

---------- Post updated at 07:23 PM ---------- Previous update was at 07:18 PM ----------

Many thanks. I walked through the code and also your comments. I now understand why my script kept on giving very few results. Most importantly I learnt how to hash data in Perl. Aia's script was the add-on. Thanks to forum members for help and above all the pains people take to correct and comment on code written by people struggling with code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate and sort to remove duplicates

Following is the input. 1st and 3rd block are same(block starts here with '*' and ends before blank line) , 2nd and 4th blocks are also the same: cat <file> * Wed Feb 24 2016 Tariq Saeed <tariq.x.saeed@mail.com> 2.0.7-1.0.7 - add vmcore dump support for ocfs2 * Mon Jun 8 2015 Brian Maly... (4 Replies)
Discussion started by: Paras Pandey
4 Replies

2. Shell Programming and Scripting

Sort and Remove duplicates

Here is my task : I need to sort two input files and remove duplicates in the output files : Sort by 13 characters from 97 Ascending Sort by 1 characters from 96 Ascending If duplicates are found retain the first value in the file the input files are variable length, convert... (4 Replies)
Discussion started by: ysvsr1
4 Replies

3. Shell Programming and Scripting

Bash - remove duplicates without sort

I need to use bash to remove duplicates without using sort first. I can not use: cat file | sort | uniq But when I use only cat file | uniq some duplicates are not removed. (4 Replies)
Discussion started by: locoroco
4 Replies

4. Shell Programming and Scripting

Help in modifying existing Perl Script to produce report of dupes

Hello, I have a large amount of data with the following structure: Word=Transliterated word I have written a Perl Script (reproduced below) which goes through the full file and identifies all dupes on the right hand side. It creates successfully a new file with two headers: Singletons and Dupes.... (5 Replies)
Discussion started by: gimley
5 Replies

5. Shell Programming and Scripting

remove duplicates and sort

Hi, I'm using the below command to sort and remove duplicates in a file. But, i need to make this applied to the same file instead of directing it to another. Thanks (6 Replies)
Discussion started by: dvah
6 Replies

6. Shell Programming and Scripting

need Shell script for Sort BASED ON FIRST FIELD and PRINT THE WHOLE FILE WITHOUT DUPLICATES

Can some one provide me a shell script. I have file with many columns and many rows. need to sort the first column and then remove the duplicates records if exists.. finally print the full data with first coulm as unique. Sort BASED ON FIRST FIELD and remove the duplicates if exists... (2 Replies)
Discussion started by: tuffEnuff
2 Replies

7. Shell Programming and Scripting

modifying perl script

Hi freinds I have a small problem I want u to help me in, I have a syslog server and configured it to send me email automatically, I get a small perl script to help me in, and tested it to send alerts to root and it worked successfully without any problems Now I want to send it outside, I... (4 Replies)
Discussion started by: reaky
4 Replies

8. UNIX for Dummies Questions & Answers

removing duplicates and sort -k

Hello experts, I am trying to remove all lines in a csv file where the 2nd columns is a duplicate. I am try to use sort with the key parameter sort -u -k 2,2 File.csv > Output.csv File.csv File Name|Document Name|Document Title|Organization Word Doc 1.doc|Word Document|Sample... (3 Replies)
Discussion started by: orahi001
3 Replies

9. Shell Programming and Scripting

Sort, Uniq, Duplicates

Input File is : ------------- 25060008,0040,03, 25136437,0030,03, 25069457,0040,02, 80303438,0014,03,1st 80321837,0009,03,1st 80321977,0009,03,1st 80341345,0007,03,1st 84176527,0047,03,1st 84176527,0047,03, 20000735,0018,03,1st 25060008,0040,03, I am using the following in the script... (5 Replies)
Discussion started by: Amruta Pitkar
5 Replies

10. Shell Programming and Scripting

Removing duplicates [sort , uniq]

Hey Guys, I have file which looks like this, Contig201#numbPA Contig1452#nmdynD6PA dm022p15.r#CG6461PA dm005e16.f#SpatPA IGU001_0015_A06.f#CG17593PA I need to remove duplicates based on the chracter matching upto '#'. for example if we consider this.. Contig201#numbPA... (4 Replies)
Discussion started by: sharatz83
4 Replies
Login or Register to Ask a Question