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

Hello, all.
I have a Diff question I'm hoping you all can help me with. I'm working with two files, which have some of the same data in them, and I need to be able to apply changes made in one file to the other one. I'm hoping to be able to use something like "patch", but in that case, I need to get a proper diff, which I'm stuck on.

The situation: The first file is a device configuration file, the second is a file that has just one section of the device's configuration. My users will be able to edit the second file (with just the partial config), which I will then need to be able to apply to the first file. Since this is a config file for a device, I can't put anything like marker symbols into the config, since the device won't accept them. Also, in the long term, I'd like to be able to take the configuration section from the user and apply it to multiple device configuration files, which is why I'm looking for something automated.

I've looked at Text::Diff and Algorithm::Diff, but since the line numbers (and total content) will be different between the two files, it doesn't look like either one of those will work. I thought about making a fake file from the main config file containing just the lines that match & then using Text::Diff, but then I'm faced with the problem of finding the matching lines, by which point I will have basically re-written Diff.

Do any of you have any ideas to make this simple and/or elegant? I can do brute force & ignorance, but I was hoping to avoid that approach.

Thanks much.

Replies are listed 'Best First'.
Re: Making a Diff from a partial change
by ysth (Canon) on Jun 08, 2005 at 18:42 UTC
    (I'm assuming here that you don't have direct access to what the changed section looked like before: if you did, diffing that to the new version with plenty of context and applying it with patch is trivial.) If you can identify with certainty which part of the big file is the changed section, you can replace it with their changes. If you can't identify it with certainty, there isn't anything I see that will solve the problem for you unambiguously.

    I think you are going to have to show what this data looks like to get help.

      Thanks for the answer.
      I do have access to the previous version of both files (I'm storing all of this in a Subversion repository), but there may be other issues. The files are firewall configuration files, Cisco PIX, to be exact. So, they contain data like:
      access-list testing permit ip host 3.3.3.3 host 22.2.2.2
      access-list testing deny tcp object-group first host 4.4.4.4 eq 99
      
      or:
      object-group network first
        network-object host 9.9.9.9
      
      The thing is, I'm not sure I'm going to be able to get enough context to dump it straight to patch...some of these sections may be as short as one or two lines.

      What I'm trying to do here is give some users the ability to edit certain parts of device config, but limit their ability to edit other parts. (And, hopefully, apply some of these snippets to multiple devices, to make them all act the same.)

        if you're giving the user the ability to edit the section, then i assume you don't want to merge the results of the edits into the same section of the other file, but you merely want to replace the whole section. do this simply by having 1 or 2 other files which have all the text excluding that section, and join it with the edited section. i.e. new file will contain: constant text 1, edited new section, and then contant text 2
        the hardest line to type correctly is: stty erase ^H