function to generate the text files for each group