Assuming you are using ksh, then it is quite simple:-
typeset -Z5 counter=1
while [ some condition ]
do
output > filename.$counter
((counter=$counter+1)) # Simple ksh 'add 1'
done
The typeset command states that this is a 5 character variable with leading zeros.
The code could be elaborated to generate 1st_file.00001, 2nd_file.00002 etc, but I haven't written that as I doubt it is actually necessary. If it is, just ask.
$ for i in *; do j=`expr $j + 1`; echo "$i" | nawk -v seq="$j" '{printf("%s %05i\n",$1,seq)}'; done > output.txt
$ while read name suffix; do echo mv $name $name.$suffix; done < output.txt
Once, you are satisfied with the above echo command, you can remove the echo command.
If this is a copy & paste of your script, then there is a spelling mistake in the typeset command. This also assumes you are running in korn shell. It seems to work fine here, but if you are still stuck, do write back.