How can I diff two config files?

14,204

Solution 1

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.

Solution 2

If you are somewhat comfortable with vim, I would strongly encourage you to use vimdiff:

vimdiff file1 file2

This will open a vim session with two panes, with one file on each side. Highlights and color will indicate differences between the files, and all identical parts will be hidden (folded, but expandable).

Then, if you want to selectively merge differences from one file to the other, you can use the following commands:

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

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

]c to advance to the next block with differences

[c to reverse search for the previous 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

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

u to undo

zo to unfold/un-hide text

zc to re-fold/re-hide text

zr will unfold both files completely (use :help fold for more about folding )

:diffupdate will re-scan the files for changes

As you start moving changed text over or bringing changes in, the now-identical parts of the files will automatically fold, too.

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

You can also write, quit, discard changes, etc., one pane at a time just as you would normally do with vim.

You can use all the common vim commands to edit the files at will ; I've only described the most common and useful commands you're likely to use in a vimdiff session (as opposed to a generic vim one).

Solution 3

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

Solution 4

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.

Solution 5

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 :)

Share:
14,204

Related videos on Youtube

jldugger
Author by

jldugger

DevOps Engineer

Updated on September 17, 2022

Comments

  • jldugger
    jldugger almost 2 years

    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?

    • Xerxes
      Xerxes about 15 years
      Watch out jldugger! You're about to level! =)
    • AnonymousLurker
      AnonymousLurker over 11 years
      It is really a bad idea to strip comments, how do you know they are irrelevant without looking at them?
  • Clinton Blackmore
    Clinton Blackmore about 15 years
    Beyond Compare is awesome!
  • Govindarajulu
    Govindarajulu about 15 years
    +1 for providing a single-line solution
  • henkojinko
    henkojinko about 15 years
    is this available on *nix systems?
  • Mark Norgren
    Mark Norgren about 15 years
    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
  • Avery Payne
    Avery Payne about 15 years
    +1 for meld, which has made graphical diff'ing sooo much more easy.
  • Zoredache
    Zoredache about 15 years
    @jldugger, try updating the grep to be like this to exclude comments and whitespace. - egrep -v '^(#.*|)$'
  • Jpsy
    Jpsy over 9 years
    I couldn't live without this tool anymore! An extreme time saver. When I changed from PC to Mac about 1 year ago I was very happy to find that it had just been ported to Mac too.