Estrutura de dados
%h = (
...
B => [a, b],
A => [1, 2, 3],
...
);
perl -F':' -lane '
push @{$h{$F[0]}}, $F[1]}{
$"=",";
print "$_:", "@{$h{$_}}|", scalar @{$h{$_}} for keys %h;
' File1 > File1.new
Breve
The field separator is set to a semicolon, thus populating each time a line is read in afresh
the @F array. Then we append the 2nd field, $F[1], to the array of hash
keyed in on the 1st field, $F[0]. At the end, we display the key name,
followed by the array contents corresponding to this key, & the count of
the array as well.
Saída
A:1,2,3|3
B:a,b|2
C:pp|1
D:rr|1
Sed
sed -e '
:loop
$!N
s/^\(\([^:]*\):.*\)\n:\(.*\)/,/
tloop
P;D
' yourfile