This script has obvious syntactical errors.
> I can't say that I understand the loop specification format, or even the overall behaviour (someone must have helped me), but this script was in the directory and appears to be related to the task...
You need information in the lines that you can only determine by later
lines, so you need to (temporarily) store the contents of the lines as
you seem to have tried.
>
> Could anyone help me to fix this?
No, because there's a much simpler and more obvious solution; two-pass
processing across your (sorted) data.
awk '
NR==FNR { n[$1]++ ; next }
{ print n[$1], $0 }
' tempid tempid >tempid2
Janis
>
> Many many thanks.
>