awk '{ i=i+1 if (i > 5) { i = 1 } filename="file" i ".txt" print >filename }' tmp.txt