Just try adding >counter".txt"
to your print statements.
for example:
printf "Thread dump #%d\n", counter >counter".txt";
Every print statement should be appended with this string as mentioned above. And then try running the awk script.
Some flavours of unix do not support this syntax. Did you put a semi colon at the end of the line or not?
In such cases do this:
file=counter".txt";
printf "Thread dump #%d\n", counter >file;