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*
In reply to Re: How do I remove duplicate numeric elements of an array and preserve alphabetic elements? -- oneliner
by Discipulus
in thread How do I remove duplicate numeric elements of an array and preserve alphabetic elements?
by jzelkowsz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |