SoFunction
Updated on 2025-04-07

Perl script code to remove duplicate content (repeat lines + array duplicate fields)


#!/bin/perl
use strict; 
use warnings; 
my $filename; 
my %hash; 
my @information; 
my $key1; 
my $key2; 
print "please put in the file like this f:\\\\perl\\\\\n"; 
chomp($filename=<STDIN>); 
open(IN,"$filename")||die("can not open"); 
while(<IN>) 

   chomp; 
   @information=split/\s+/,$_; 
   if(exists $hash{$information[0]}{$information[1]}) 
   { 
       next; 
   } 
   else 
   { 
       $hash{$information[0]}{$information[1]}='A'; 
    } 
   } 
   close IN; 
   open(IN,"$filename")||die("can not open"); 
   while(<IN>) 
   { 
       @information=split/\s+/,$_; 
       if(exists $hash{$information[1]}{$information[0]}) 
       { 
           delete $hash{$information[0]}{$information[1]} 
       } 
       else 
       { 
           next; 
       } 
   } 
   close IN; 
   open(OUT,">f:\\A_B_result.txt")||die("can not open"); 
   foreach $key1 (sort{$a<=>$b} keys %hash) 
   { 
       foreach $key2 (sort{$a<=>$b} keys %{$hash{$key1}}) 
       { 
           print OUT "$key1 $key2\n"; 
       } 
   } 
close OUT;