Implement top value per group sort in bash

Viewed 174

Using bash, I'd like to sort and filter a list to retain only the last patch version (top value) per minor version (per group). For example:

$ cat data | sort -Vr
3.10.1
3.10.0
3.3.10
3.3.2
3.3.1
3.2.0
3.1.0
3.0.3
3.0.2
3.0.1
3.0.0

I'd like to get this result:

3.10.1
3.3.10
3.2.0
3.1.0
3.0.3

How to do it?

1 Answers

Use another sort with -u

sort -Vr data | sort -ru -t. -Vk1,2
  • -t. uses a dot as the field separator
  • -k1,2 sorts by the first two fields, i.e. it ignores the patch version
  • -u means "unique", i.e. it only prints the first occurrence for each group that sorts the same
Related