Skip to main content
Post Made Community Wiki by don_crissti
Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

With gnu datamash:

datamash -H -s -g 1 mean 2 <file
GroupBy(Id) mean()
510 64.4
601 24

This sorts and groups by 1st field calculating 2nd field mean value, preserving Headers. It assumes the fields are separated by single tab. Use -W, --whitespace if they're separated by multiple blanks or -t, --field-separator= to define another field separator (space, comma etc). Since datamash requires sorted input, the output will be sorted by the grouped column.