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?

Zach Smith
  • 155
  • 3
  • 9
  • 1
    "Am I reading the output correctly?" What was the output you got? – muru Aug 20 '18 at 07:45
  • I thought the output was the same, but there was a small difference that I couldn't easily see. The answer to this question was perfect! – Zach Smith Aug 20 '18 at 08:06

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 1
    That's why GNU diff has a rather large number of options dealing with whitespace. – muru Aug 20 '18 at 08:15
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
  • 197,895
  • 55
  • 485
  • 740