Although the above answer is the better solution IMO (and I have upvoted), this is an alternative solution I helped create a few years back. The scenario was similar, a fixed-width file needed to be generated and extracted twice a day.
The solution was to build a custom object where each record stored a line of the file - seems odd but the file could be of varying sizes and there were different 'types' of line so the ordering was important too.
Twice a day a batch file (it had to be a Windows box unfortunately) would fire up dataloader to pull out the lines in the correct order and then it'd use a command line substitution tool (similar to sed
) to do a bit of cleanup (removing the CSV parts) before moving the file into another directory where it was needed.
The batch would then use the success log to update the downloaded records setting a checkbox to true, and those would subsequently be deleted from the system by a scheduled batch apex job.