1

I have managed to list the git tags with the help of the following command:

git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)'

as described in How can I list all tags in my Git repository by the date they were created?. The result looks as follows:

4.18.038
4.18.039
4.18.040
4.18.041
4.18.042
4.18.043
4.18.044
4.19.001
4.19.002
4.19.003
4.19.004
4.19.005
4.19.006
4.19.007
4.19.008
4.19.009
4.19.010
4.19.011
4.19.012
4.19.013
4.19.014
4.19.015
4.19.016
4.19.017
4.19.018
4.19.019
4.20.000

I would like to select only the last occurrence(latest) of each version. Eg.

4.18.044
4.19.019
4.20.000

Any ideas how I could achieve that?

PS. I am using Windows 7 with MINGW32

Community
  • 1
  • 1
karmatic
  • 81
  • 4

4 Answers4

3
$ awk -F. '{a[$1,$2]=$0} END{for (i in a) print a[i]}' test
4.18.044
4.19.019
4.20.000
  • -F. sets the field seperator as .

  • 'a[$1,$2]=$0 assigns the value of entire record to an array a indexed by the first 2 columns, $1, $2

  • for (i in a) print a[i] prints the content of the array

Caution

This works only if the file is sorted. If not you can use sort and pipe the result to awk

$ sort test | awk -F. '{a[$1$2]=$0} END{for (i in a) print a[i]}' 
4.18.044
4.19.019
4.20.000
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
  • 1
    this is fine... as long as the list is already sorted, of course. If not, you can use `sort -V` first, as sputnick mentioned in his answer. – fedorqui Dec 01 '14 at 14:46
  • 1
    @fedorqui Good point !! I have edited my answer to include that as well. Thank you:) – nu11p01n73R Dec 01 '14 at 14:51
  • git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)' | sort | awk -F. '{a[$1$2]=$0} END{for (i in a) print a[i]}' worked like a charm. Thank you! – karmatic Dec 01 '14 at 16:05
  • Never use an array index like `a[$1$2]`, use `a[$1,$2]` instead. Consider the difference in behavior of each if the input contained the lines `a bc` and `ab c`. – Ed Morton Dec 01 '14 at 16:11
1

What I would do using sort to ensure that the version list is well sorted :

sort -V file | awk -F '.' '{arr[$2]=$0}END{for (a in arr) {print arr[a]}}'
4.18.044
4.19.019
4.20.000
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

Using sort only:

~$ sort -r f | sort -t. -k1,2 -u
4.18.044
4.19.019
4.20.000

the -t specifies the delimiter, the -k1,2 the fields and -u prints only 1 of them

fredtantini
  • 15,966
  • 8
  • 49
  • 55
1
tac file | awk -F'.' '!seen[$1,$2]++' | tac
Ed Morton
  • 188,023
  • 17
  • 78
  • 185