Ok, there were few differences between the one liner and the developed code.
But still I am not sure I understand how it works...
It works except when $1=4. It should be treated the same as when $1<4.
In other words, for each groups, if the max value of $1 is <=4, then a "*" must be added at the end of the Ref record of the group.
If the max value of $1 in a group is >4, then the Ref record is unchanged.