Question: Have a file with below pattern

```
a 12
a 13
a 15
a 14
b 5
b 6
c 2
c 5

```

I'd like to group these according to the first column, like below

```
a 12 13 15 14
b 5 6
c 2 5

```

Please suggest

Answer: 
Assuming that the data is sorted by the first column, as it is in the question, using GNU `datamash` to group by the first whitespace-delimited column and collapse the second.

```bsh
$ datamash -W groupby 1 collapse 2 <file
a       12,13,15,14
b       5,6
c       2,5

```

Replacing the tab after the first column in the result as well as the commas, by spaces, to arrive at the wanted output:

```bsh
$ datamash -W groupby 1 collapse 2 <file | tr '\t,' '  '
a 12 13 15 14
b 5 6
c 2 5

```

If the input is not sorted by the first column, then pass the data through `sort` first, or use `datamash` with its `-s` (`--sort`) option.
Python module itertools has a method (groupby) which does exactly what you want. You just give it an iteratble (file handle) and a function to group on (lambda aka, anonymous or inlined function, grouping on first column(x.split()[0])

```
python3 -c 'import sys, itertools as it
with open(sys.argv[1]) as f:
  for k,igrp in it.groupby(f,lambda x: x.split()[0]):
    print(k,*["".join(e.split()[1:]) for e in igrp])
' file

```

---

Using GNU sed with extended regex mode (-E) we accumulate the consecutive lines having the same first field without printing. Then when a transition occurs, print the first portion, remove it, and cycle repeats

```
sed -E ':a
  $!N
  #  12   2    1       3    3
  s/^((\S+)\s.*)\n\2\s+(\S.*)/\1 \3/;ta
  P;D
' file

```

---

Using awk we set the ORS to empty

```
awk -v ORS= '
NR==1 || prev != $1 {
  if (NR > 1) print RS
  prev=$1;print
  next
}
{ $1="";print }
END { print RS }
' file

```

---

Perl using the ellipsis operator (..) in a scalar context to get a range of lines.

```
perl -lane '$,=$";
  my $e = ($p=$F[0],1) .. ($F[0] ne $p);
  if ($e =~ /E0/) {
    print $p, splice @A;
    $p = $F[0];redo;
  }
  push(@A,splice(@F,1));
  }{print $p, @A;
' file

```

---

Output:-

```
a 12 13 15 14
b 5 6
c 2 5

```