suhailck has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

My text file has input of the form

abc dft45.xml
ert rt653.xml
abc ert57.xml

I need to write a perl script to find duplicates in the first column and write it into a text file of the form...

abc dft45.xml
abc ert57.xml

Can some one help me plz?

Thanks in Advance

Replies are listed 'Best First'.
Re: find duplicates and write to file
by ikegami (Patriarch) on Jun 30, 2010 at 16:48 UTC
    my %seen; while (<>) { my ($key) = /^(\S+)/; push @{ $seen{$key} }, $_; } for my $key (keys(%seen)) { my $vals = $seen{$key}; print @$vals if @$vals > 1; }
Re: find duplicates and write to file
by petecm99 (Pilgrim) on Jun 30, 2010 at 18:59 UTC
    This is not nearly as elegant as ikegami's, but just another alternative...basically stores your input file into a hash with the first column as the key, incrementing the hash element each time it is encountered.

    my ($col1, $col2); my %index = (); open INFILE, "<", "textfile.txt" or die "Can not open textfile.txt"; while ( <INFILE> ) { chomp; ($col1, $col2) = split (/ /); if (not exists $index{$col1}) { $index{$col1} = 1; }else{ $index{$col1}++; } close INFILE; open OUTFILE, ">", "outfile.txt" or die "Can not open outfile.txt"; foreach my $value (sort keys %index) { if ($index{$value} > 1) { print OUTFILE "$value\n"; } } close OUTFILE;
    Update: This only outputs the first column - need a bit more to plop out the entire line of input...