Remove not only the duplicate string but also the keyword of the string in Perl

Hi Perl users,

I have another problem with text processing in Perl. I have a file below:

Linux   Unix   Linux  Windows SUN
MACOS  SUN  SUN HP-AUX

I want the result below:

Unix     Windows   SUN
MACOS   HP-AUX

so the duplicate string will be removed and also the keyword of the string on that line.

I have a code below so far but the problem hasn't solved yet.

#!/usr/bin/perl

use strict;
use warnings;

my $replacement = "";
my (@temp, @data, %result_of_hash) = ();

while (<>)
{  
  chomp;
  @temp = sort split " ", $_;

  my $prev_line = undef;
  for my $i (0 .. (scalar @temp -1))
  {
    if ($temp[$i] eq $prev_line)
    {
       push @data, "$temp[$i]";
    }
  
  $prev_line = $temp[$i];
  }

  $store{$line} = 1;  
}


foreach my $key_store ( keys %store )
{
  for my $k (@data)
  {
         my $pos = index($key_store,$k);
        if ($pos > -1)
    { 
       my $result = substr ($key_store, $pos, length($k),$replacement);
       $result_of_hash{$key_store} = 1;
    }
  }    
}

print "$_\n" for keys %result_of_hash;
close (FH);

Hi,
you can use this way:

$ cat in.txt
Linux   Unix   Linux  Windows SUN
MACOS  SUN  SUN HP-AUX
$ perl -sane 'foreach $i (@F) { $X{$i}+=1 } ; foreach $i (@F) {printf "%s ",$i if $X{$i}==1 };print "\n";%X=""' in.txt
Unix Windows SUN
MACOS HP-AUX

Regards.

1 Like

Hi disedorgue,

Thanks for the script, it works for one-liners but why the script is not work when I try to modify the script as below.

#!/usr/bin/perl -w
use strict;

my $file = "in.txt";
my %X = ();

open (FH, '<', $file) or die $!;

my @F = <FH>;

foreach my $i (@F) 
{ 
  $X{$i}+=1; 
}  

foreach my $i (@F) 
{
  
  if ($X{$i}==1)
  {
    printf "%s ","$i";
  } 
}
print "\n";
%X= ();

close (FH);

---------- Post updated at 08:09 AM ---------- Previous update was at 06:36 AM ----------

Hi,
it works with small modification. Thanks

#!/usr/bin/perl -w
use strict;

my %X = ();
open (FH, '<', "in.txt") or die $!;

my @F = <FH>;
my @temp;

foreach my $line (@F) 
{ 
  chomp $line;
  @temp = split " ", $line;  
  for my $i ( 0 .. $#temp)
  {
    $X{$temp[0]}{$temp[$i]}++;    
  }
} 

foreach my $key(keys %X)
{ 
  foreach my $subkey(keys %{$X{$key}})
  {
     print "$subkey " if $X{$key}{$subkey} == 1;
  }
  print "\n";
}

close (FH);