Ubuntu: Diff reports difference but both lines are the same (3 solutions!)
Channel: Roel Van de Paar
Date: 2019-06-21

Ubuntu: Diff reports difference but both lines are the same


The Question: I diff-ed 2 files and got
1c1
＜ 1
---
＞ 1
Both files contained just "1". How is this different?

Solutions Sample (Please watch the whole video to see all solutions, in order of how many people found them helpful):

== This solution helped 68 people ==
1st line: a stands for added, d for deleted and c for changed. Line numbers of
the original file appear before these letters and those of the modified file
appear after the letter.
2nd line: line with ＜ are from file 1 and are different from file 2.
3rd line is a divider.
4th line: line with ＞ are from file 2 and are different from file 1.
(If you ever see = it means the lines are the same in both files)
And your problem might be whitespaces or other non-human readable characters:
those trigger a difference too.
There are some options to manipulate output.
Example:
rinzwind@discworld:~$ more 1
test
test2
test3
rinzwind@discworld:~$ more 2
test
test2
test3
contexted format:
rinzwind@discworld:~$ diff -c 1 2
*** 1 2011-08-13 17:05:40.433966684 +0200
--- 2 2011-08-13 17:11:24.369966629 +0200
***************
*** 1,3 ****
test
! test2
test3
--- 1,3 ----
test
! test2
test3
A "!" represents a change between lines that correspond in the two files. A "+"
represents the addition of a line, while a blank space represents an unchanged
line. At the beginning of the patch is the file information, including the full
path and a time stamp. At the beginning of each hunk are the line numbers that
apply for the corresponding change in the files. A number range appearing
between sets of three asterisks applies to the original file, while sets of
three dashes apply to the new file. The hunk ranges specify the starting and
ending line numbers in the respective file.
Expanding on Lekensteyn's comment about unified format:
rinzwind@discworld:~$ diff -u 1 2
--- 1 2011-08-13 17:05:40.433966684 +0200
+++ 2 2011-08-13 17:11:24.369966629 +0200
@@ -1,3 +1,3 @@
test
-test2
+test2
test3
The format starts with the same two-line header as the context format, except
that the original file is preceded by "---" and the new file is preceded by
"+++". Following this are one or more change hunks that contain the line
differences in the file. The unchanged, contextual lines are preceded by a
space character, addition lines are preceded by a plus sign, and deletion lines
are preceded by a minus sign.
Some useful options:
-b Ignore changes in the amount of white space.
-w Ignore all white space.
-B Ignore all blank lines.
-y output in 2 colums.

== This solution helped 1 person ==
i had the same problem and i found a solution that might help, use the command
:
 dos2unix ＜file1＞ ＜file2＞
one of them might be in a dos/windows format and the other one UNIX format
after i did this the diff was all good!

With thanks & praise to God! With thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Rinzwind (https://askubuntu.com/users/15811), user levk (https://askubuntu.com/users/189747), user lesmana (https://askubuntu.com/users/1366), user Jiew Meng (https://askubuntu.com/users/14782), user Charles Boling (https://askubuntu.com/users/235811), and the Stack Exchange Network (http://askubuntu.com/questions/56964). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything looks amiss at Roel D.OT VandePaar A.T gmail.com.