I have two files that essentially contain a memory dumps in a hex format. At the moment I use diff to see if the files are different and where the differences are. However, this can be misleading when trying to determine the exact location (i.e. memory address) of the difference. Consider the following example showing the two files side-by-side.

file1:       file2:

0001    |    0001
ABCD    |    FFFF
1234    |    ABCD
FFFF    |    1234

Now diff -u will show one insertion and one deletion, although 3 lines (memory locations) have changed between the two files:


Is there an easy way to compare the two files such that each line is only compared with the same line (in terms of line numbering) in the other file? So in this example it should report that the last 3 lines have changed, along with the changed lines from file1 and file2. The output doen't have to be diff-style, but it would be cool if it could be colored (at the moment I color the diff -u output using sed so that could easily be adapted).


This could be an approach:

diff <(nl file1) <(nl file2)

With nl number the lines that diff recognizes the lines line by line.

  • 3
    That's cool, I didn't know the nl program. Those unix tools are really handy, but there are so many of them. – Fritz Jun 10 '14 at 11:04
  • I just tried this, and unfortunately what I get is: every line of the first file, followed by every line of the second file... instead of line 1 from file 1, followed by line 1 from file two, followed by line 2 from file one, followed by line 2 from file 2... – Michael Hewson Jan 18 '18 at 0:46
  • @MichaelHewson It sounds like your two files have no lines in common. In that case, that's just how diff works. You might try the gui program meld which shows the two files side by side. As in meld <(nl file1) <(nl file2) – Fritz Aug 24 '18 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.