RaduH has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

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
    Seems like a lot of trouble only for this. Any other solution?
    It just seems like a lot of work because you wrote it in English instead of (perl) code. Also, writing the output somewhere else and then replacing the original is safer (and much less work) than modifying the original file in place.

    If you have enough memory you can probably do it in one or two lines of code. But I suspect this is homework, so I'm not going to elaborate. See the -i & -p & -0 flags

    updated: forgot to add the -0 flag.

      Hi Joost,

      I wish this was homework. I've been out of school for enough years not to get upset by the word "homework" anymore :). The "enough memory" solution I know, but I would rather avoid. I've learned the lesson "64k should be enough for everybody" soon after is was said :), so I am trying to avoid getting my head into this gutter.

      The real application for this is messing with /etc/fstab file and changing the location where some partitions are mounted (i.e., flip two partitions). I made up the Name-Address simplified example so the discussions won't get off track and discuss the Unix problem rather than my Perl question.

      Thanks for your suggestion, I'll look at that link!

Re: In place file modification
by GrandFather (Saint) on Nov 01, 2007 at 01:15 UTC

    You may find Tie::File helps. When you have actually written some code and if you have trouble getting it to work come back and ask for more help. Remember to bring your code though.


    Perl is environmentally friendly - it saves trees
      Thanks! I'll look at that.
Re: In place file modification
by dragonchild (Archbishop) on Nov 01, 2007 at 13:59 UTC
    Most programs involve a lot of infrastructure that can be reused for other purposes. This is why we have modules and design patterns. What you're looking at as a lot of work is, frankly, a lot of work. You're doing the following:
    • Open and read a file
    • Find a specific item in that file
    • Parse the line and pull out another piece of information
    • Find another specific item in the file
    • Parse that line and pull out another piece of information
    • Swap the two
    • Rewrite the file
    • Close the file

    That you're dealing with names and addresses is irrelevant to the rest of the work you have to do. Just imagine how much work you would have to do if you didn't have open(), <>, regexes (or split), hashes (or arrays), and print() ... this is why we use Perl.

    Now, once you have this infrastructure, there's a lot of different tasks that become a lot easier. Proper design and decomposition avoids bugs you didn't realize were possbile and enables features that you didn't even imagine you could have for as little effort as they end up being. I've lost count of the number of times I've told a boss "Because of doing things the right way, we now have some major feature that no-one even thought about that can now define our product."


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?