Friday, January 28, 2011

How can I diff two config files?

I've got two snmpd.conf files, one on a server that works, and one that doesn't. How can I diff the two config files while stripping out irrelevant comments and newlines?

  • There might be a more elegant way to do it, but pragmatically (and quickly):

    grep -v '^#' server1-snmpd.conf | grep -v '^ *$' > server1-snmpd.conf-clean
    grep -v '^#' server2-snmpd.conf | grep -v '^ *$' > server2-snmpd.conf-clean
    diff server1-snmpd.conf-clean server2-snmpd.conf-clean
    
    From jj33
  • diff <(grep -v '^#' f1) <(grep -v '^#' f2)
    

    To avoid blank lines, and lines containing nothing but spaces, in addition to identical lines that have a single difference of added leading spaces...

    diff -b \
      <(grep -vE '^([ \t]*#|^[ \t]*$)' f1)\
      <(grep -vE '^([ \t]*#|^[ \t]*$)' f2)
    

    By this point though, I'd probably put that into a script and write something like the original suggestion that's a little more readable.

    wzzrd : +1 for providing a single-line solution
    jldugger : Any ideas on how to omit lines containing whitespace as well? Turns out once you cut out comments there's lots of blank lines spacing them out.
    Zoredache : @jldugger, try updating the grep to be like this to exclude comments and whitespace. - egrep -v '^(#.*|)$'
    From Xerxes
  • If you are somewhat comfortable with vim, I would strongly suggest to use vimdiff:

    vimdiff file1 file2
    

    That will open a split window with a file at each side, and all identical parts on both files folded.

    Then, if you want to bring or put the differences from one file to the other, you can use the following commands:

    (Consider the "current file" the one where the cursor is)

    ^W^W to change the cursor from one file's window to the other file's window

    ]c to advance to the next block with differences

    do (diff obtain) to bring changes from the other file to the current file

    dp (diff put) to send changes from the current file to the other file

    u to undo

    Both do and dp work if you are on a block or just one line under a block.

    As you start sending or bringing the changes, the (now) identical parts will start to fold too.

    When you are finished, you can quit writing both files with :xa!

    Now, of course you can use all the common vim commands to edit the files at will, I just described the most useful commands in a vimdiff setup.

    If you don't like the automatic folding, with zr you can unfold both files completely (see :help fold for more commands on folding )

    From
  • Beyond Compare is the ultimate tool for this!

    Link: http://www.scootersoftware.com/

    Available for Windows and Linux.

    Jeff wrote a good overview article about the tool awhile back:
    http://www.codinghorror.com/blog/archives/000454.html

    Clinton Blackmore : Beyond Compare is awesome!
    Preet Sangha : is this available on *nix systems?
    marked : Beyond Compare 3 does not run as a console application on Linux. It requires X-Windows. Supported Linux distributions (32-bit) Red Hat Enterprise Linux 4, 5 Fedora 4 - 10 Novell Suse Linux Enterprise Desktop 10 openSUSE 10.3, 11 Ubuntu 6.06 - 8.10 Not Tested Any 64-bit Linux kernel Not Compatible Red Hat Enterprise Linux 3
    From marked
  • If you're using a bash-like shell, you can try this:

    # Name this diff-stripped
    STRIPPED=
    for i in $*; do
        egrep -v "^#|^\s*" "$i" > "$i.stripped"
        STRIPPED="$STRIPPED $i.stripped"
    done
    
    diff $STRIPPED
    

    Then invoke it like this:

     diff-stripped file1 file2 ...
    

    You can also change diff to vimdiff or gvimdiff which both come with vim.

    From Neil
  • Expanding on nima's one-liner, you could do that as a shell function and drop it in your .bashrc

    diff <(grep -v '^#' f1) <(grep -v '^#' f2)
    

    becomes (using -u because I like unified diffs)

    function cleandiff {
      diff -u <(grep -v '^#' $1| grep -v '^ *$') <(grep -v '^#' $2 | grep -v '^ *$')
    }
    

    If you like GUI diff viewers, meld is nice, and understands revision controlled dirs/files.

    Avery Payne : +1 for meld, which has made graphical diff'ing sooo much more easy.
  • After cleaning the comments, I would advise using KDiff3, it's a pretty good diff/merge tool and you dont need vim fu to use it :)

  • This is the same as nima's one liner, but will filter out blank lines too as somebody requested.

    diff -u <(egrep -v '^(#| *$)' f1) <(egrep -v '^(#| *$)' f2)
    

    (I'd also install colordiff if possible and use that in place of normal diff)

    From Mark
  • I use WinMerge http://winmerge.org to diff files, granted I have to pull them down to my machine, but it works for.

0 comments:

Post a Comment