There are many ways to do this depending on the scripting language you are using; or if you are simply using a command line.
If you are simply using a command line, you can redirect to a file like this:
Code:
command > output.txt
If you want to append to the same file, you can use:
Code:
command >> output.txt
You can do this in a script so many, many ways; depending on your skill and tools; PERL, PHP, SED, AWK, etc etc.