in reply to How do I remove duplicate numeric elements of an array and preserve alphabetic elements?
As you already got useful answers I propose you a short version(pay attention to windows double quotes!):
perl -F"," -lane "push @{$h{$F[0]}},$F[1]}{print map{$_.'|'.(join',',@ +{$h{$_}}).qq(\n)}keys %h" data.txt 20155505|YOUSLAV,YURT,TENWIMPL 20102741|WEDLOFOU,YOUSLAV,YURT,KUPLYSO,TENWIMPL 20011541|YOUSLAV,TENWIMPL 20011271|YOUSLAV,WUMARTHE 20147155|YOUSLAV,KUPLYSO,FRIMA 20055111|YOUSLAV,YURT,TENWIMPL
See perlrun to get all these perl switches explained, but use -MO=Deparse to see the oneliner exploded and more readable (the curly braces in $F[1]}{print are a trick called esquimo greeting;):
perl -MO=Deparse -F"," -lane "push @{$h{$F[0]}},$F[1]}{print map{$_.'| +'.(join',',@{$h{$_}}).qq(\n)}keys %h" BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(/,/, $_, 0); push @{$h{$F[0]};}, $F[1]; } { print map({$_ . '|' . join(',', @{$h{$_};}) . "\n";} keys %h); } -e syntax OK
L*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I remove duplicate numeric elements of an array and preserve alphabetic elements? -- oneliner
by jzelkowsz (Novice) on Jun 07, 2018 at 13:21 UTC |