0

I want to diff the output of the head command applied to two separate files:

diff <(head -n 1 file1.csv) <(head -n 1 file2.csv)

I got the structure of the command from a similar question: How do I diff the output of two commands?

But all that happened is that the top lines of each file were printed. So I'm not sure if it's diffing or not. Am I reading the output correctly?

How do I diff the output of the head command applied to two different files?

2 Answers2

3

diff is meant to print different lines only. The fact that you got those lines printed means they're different. Take these two as example:

# lines different, have output
$ diff <(head -n 1 /etc/passwd) <(head -n1 input.txt)
1c1
< root:x:0:0:root:/root:/bin/bash
---
> hello world
# same file, first lines aren't different, no output
$ diff <(head -n 1 /etc/passwd) <(head -n1 passwd.copy)
$

Note that even slightest change should be detected by diff, so for example if I add a single trailing space in passwd.copy those two first lines are not the same anymore, even though at first sight they look so:

$ diff <(head -n 1 /etc/passwd) <(head -n1 passwd.copy)
1c1
< root:x:0:0:root:/root:/bin/bash
---
> root:x:0:0:root:/root:/bin/bash 

See also Understanding diff output

3

If the output of diff isn't sufficiently clear, consider using a more advanced tool, like meld, or my person choice: vimdiff. Using a similar example like @Sergiy, this is what Vimdiff shows me:

enter image description here

As can be seen, the extraneous whitespace is highlighted. More advanced diff highlighting can be obtained using the DiffChar plugin (see my post on Vi & Vim SE for an example).

muru
  • 207,228