Adding a footer to a flat file

I wanted to know if there is a way to add a footer to a flat file that is being extracted by a sql query. so if the query extracts 1000 rows, in the flat file it appends the last line as 1000.
Is there a way to do so?

Thanks

If extract.txt is your flat file, this will add line 1001 to the end with a count of 1000.

trailer=`wc -l < extract.txt`
echo "$trailer" >> extract.txt