RaduH has asked for the wisdom of the Perl Monks concerning the following question:
I have a text file made of short lines (line wrapping is not an issue, in case it makes a difference, although I doubt it does). I need to make a very simple change to this file. At some point a line looks like this: Name1 Address1. Then somewhere else in the file I have Name2 Address2. All I want to do is swap Address1 and Address2, as if the two people swapped homes.
I can read the entire file in the memory (I can also read and process it line by line so I don't use a whole lot of memory), then go line by line and write it to a new file as long as it is not the Name1 line. I have to stop there and keep searching for the other one, with Name2. Then extract Address2, rebuild the Name1 line write it to file, continue writing the other lines to the new file until I reach Name2 line and then doctor that one using Address1 which I carefully stored somewhere, then copy the rest of the lines. At the end I need to replace the original file with the new file I created.
Seems like a lot of trouble only for this. Any other solution?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: In place file modification
by Joost (Canon) on Nov 01, 2007 at 00:27 UTC | |
by RaduH (Scribe) on Nov 01, 2007 at 15:18 UTC | |
|
Re: In place file modification
by GrandFather (Saint) on Nov 01, 2007 at 01:15 UTC | |
by RaduH (Scribe) on Nov 01, 2007 at 15:27 UTC | |
|
Re: In place file modification
by dragonchild (Archbishop) on Nov 01, 2007 at 13:59 UTC |