Reputation: 2365
How can I display only different rows using diff in a separate file?
For example, the file number 1 contains the line:
1;john;125;3
1;tom;56;2
2;jack;10;5
A file number 2 contains the following lines:
1;john;125;3
1;tom;58;2
2;jack;10;5
How to make in the following happen?
1;tom;58;2
Upvotes: 34
Views: 56869
Reputation: 6052
I found the following the easiest to visualise and understand, showing each file's unique lines side by side:
diff -y --suppress-common-lines "file1" "file2"
Upvotes: 2
Reputation: 526
The following command provides only differences of two files one line at a time :
diff -W 400 -ay --suppress-common-lines <(nl file1) <(nl file2) | sed 's/|'"$(printf '\t')"'\+ \+/\n/g' | sed 's/^'"$(printf '\t')"'* */\n/g' | grep -aE '[0-9]'
It displayed the location of the difference and juste the lines which are different. the -W 400 should be adjusted to the max length of line of both files. The -a is not mandatory but it is really useful in case of text files which are recognized as binary by the system.
Upvotes: 1
Reputation: 37817
Here's a simple solution that I think is better than diff
:
sort file1 file2 | uniq -u
sort file1 file2
concatenates the two files and sorts ituniq -u
prints the unique lines (that do not repeat). It requires the input to be pre-sorted.Upvotes: 25
Reputation: 484
Using group format specifiers you can suppress printing of unchanged lines and print only changed lines for changed
diff --changed-group-format="%>" --unchanged-group-format="" file1 file2
Upvotes: 9
Reputation: 42870
a.txt:
1;john;125;3
1;tom;56;2
2;jack;10;5
b.txt:
1;john;125;3
1;tom;58;2
2;jack;10;5
Use comm:
comm -13 a.txt b.txt
1;tom;58;2
The command line options to comm
are pretty straight-forward:
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)
Upvotes: 49
Reputation: 454970
Assuming you want to retain only the lines unique to file 2 you can do:
comm -13 file1 file2
Note that the comm
command expects the two files to be in sorted order.
Upvotes: 14