Merging several line in one based on an occurrence

Hi,

I try to gather several line in one based on the first column.

For exemple :

toto;1;2;3
toto;4;5;6
toto;7;8;9
etc...   (Number of lines not predefined)
ruru;a;b;c
ruru;d;e;f
ruru;g;h;i
...

I'm trying to get :

toto;1;2;3;4;5;6;7;8;9...
ruru;a;b;c;d;e;f;g;h;i...

Thank you.

$
$ cat -n f52
  1  toto;1;2;3
  2  toto;4;5;6
  3  toto;7;8;9
  4  ruru;a;b;c
  5  ruru;d;e;f
  6  ruru;g;h;i
  7  ruru;j;k;l
$
$ perl -F';' -lane 'if ($F[0] eq $p){$s .= ";".join(";",@F[1..3])} else {print $s if $.>1; $s=$_} $p=$F[0]; END {print $s}' f52
toto;1;2;3;4;5;6;7;8;9
ruru;a;b;c;d;e;f;g;h;i;j;k;l
$
$ # alternatively,
$ perl -lne '/^(\w+)(;.*)$/; if ($1 eq $p){$s .= $2} else {print $s if $.>1; $s=$_} $p=$1; END {print $s}' f52
toto;1;2;3;4;5;6;7;8;9
ruru;a;b;c;d;e;f;g;h;i;j;k;l
$
$

tyler_durden

1 Like

Thank you for the 2 solutions.