how to sort and remove duplicates with Perl

Have a list like the following:
XY-B
AB_C
ABB
ACB
ABB
XYZ
would like to get
ABB
AB_C
ACB
XY-B
XYZ
Prefer to a one-liner.
jl66Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jl66Author Commented:
The list may contain numbers. Sorry for that.
0
Adam314Commented:

my %lines;
while(<DATA>) {
	chomp;
	$lines{$_}++;
}
 
print "$_\n" foreach (sort keys %lines);
 
__DATA__
XY-B
AB_C
ABB
ACB
ABB
XYZ

Open in new window

0
Carl BohmanCommented:

perl -ne '$a=$_;s/[^a-zA-Z0-9]//g;$h{$_}=$a;END{print map {$h{$_}} sort keys %h}'

Open in new window

0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Carl BohmanCommented:
One thing that's not very clear: Do you want to sort ignoring non-alpha and non-numeric or are you just looking for a basic sort?  My last answer assumed you were ignoring the dash and underscore for sorting purposes.  If you want a basic sort, the following should work.
perl -ne 'chomp;push @a, $_;END{print join("\n", sort @a), "\n"}'

Open in new window

0
ozoCommented:
perl -lne '$s{$_}++;END{print for sort keys %s}' list
0
jl66Author Commented:
bouns, the basic sort is good enough.
ozo, if the list is a file, is the one-liner for a file different from the above? I tried to use it for a file, it gave the unexpected result.
0
ozoCommented:
in what way was the result unexpected?
0
Adam314Commented:
The sort that bounsy gives will not remove duplicates.

If you are on unix, you could use uniq and sort commands.
0
jl66Author Commented:
ozo,  if I used yours exactly, I got the error:
perl -lne '$s{$_}++;END{print for sort keys %s}' test.txt
Can't find string terminator "'" anywhere before EOF at -e line 1.
if I replaced ' with ", I got the error:
perl -lne "$s{$_}++;END{print for sort keys %s}"   test.txt
Substitution pattern not terminated at -e line 1.
0
jl66Author Commented:
Adam314, unfortunately the OS is Windows.
0
jl66Author Commented:
bounsy's solution sorted the inputs but kept the duplicates as Adam314 mentioned.
0
Adam314Commented:
On windows, the command ozo gave would be this.  Replace test.txt with the actual input file name.  If you want the output to go to a file instead, the the second command, replacing output.txt with the desired output file name.

It looks like you tried this already.  Did you try it exactly as it is there?  Or could there have been a typo?  Try using copy/paste to make sure there are no typos.


#output to screen:
perl -lne "$s{$_}++;END{print for sort keys %s}" test.txt
 
#output to file:
perl -lne "$s{$_}++;END{print for sort keys %s}" test.txt > output.txt

Open in new window

0
ozoCommented:
perl -lne "$s{$_}++;END{print for sort keys %s}"   test.txt
is correct on windows
did you omit the % on the %s?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.