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

2 Answers 2


This could be an approach:

diff <(nl file1) <(nl file2)

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

  • 5
    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, 2014 at 11:04
  • 2
    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... Jan 18, 2018 at 0:46
  • 1
    @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, 2018 at 8:30
  • Amazing solution. May 20, 2020 at 16:38
  • 1
    nl by default only numbers non-empty lines, hence this solution doesn't work as intended when files have empty lines in between non-empty lines. A better alternative is: diff <(nl -ba file1) <(nl -ba file2). May 11, 2021 at 8:20

Adapted from the meld answer this works for me at the command line sort -n -k1,1 -s <(nl file1) <(nl file2). Useful where meld isn't installed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .