If you have a list @list and you want the unique elements from it you have to use a hash :
my @list = ('c', 'c', 'a', 'a', '1', '1') ;
my %unic ;
for my $it (@list) {
$unic{$it}++
}
for my $k (sort keys %unic) {
print $k, " "
}
In your code I see a bunch of different issues, but it's difficult to propose a solution without the source file you're parsing.
@final_macaddr = qx (@sonic_macaddr | sort | uniq -d -c);
The perl idiom qx
("quote execute") forks out the execution to the OS, which does not know what @sonic_macaddr is, because it's a Perl variable ! Use a hash to "sort unic" within the Perl script.
push (@sonic_macaddr, $mac_addr."\n");
I wouldn't push the "\n" inside the list : you're much better adding it when printing ie :
for my $it (@sonic_macaddr) {
print $it, "\n" ;
}
Or even :
print join "\n", @sonic_macaddr ;
You never know when you'll need a clean list of mac addresses ;)