[Tutor] how to add a comma to the end of each line in a file

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 9 May 2001 13:59:44 -0700 (PDT)


On Wed, 9 May 2001, Rob Andrews wrote:

> I feel almost embarassed having to ask this, but I've got several files that
> need a comma added to each line, and I'm not sure what the best way to go
> about it is. Any suggestions?

One funny way we can do this is to suck the whole string in from each
file, and replace the '\n' newline sequence with ',\n' instead.  
string.replace() should work well for this purpose.  So, it might look
something like:

    for each filename in our list of files:
        let's suck the contents out with read(),
        replace all newlines with the comma-newline combination
        and rewrite our new file

Hope this helps!