in reply to Group every 50 rows

You can use $. variable instead of incremeting $counter. See perlvar.
Sorry if my advice was wrong.