Limitation: this is not a real file diff, more like a set of the lines diff (but you may need exactly this).
All differences between a.txt
and b.txt
:
sort a.txt b.txt | uniq -u > c.txt
Lines which missing from a.txt
(ignoring lines which missing from b.txt
):
sort a.txt a.txt b.txt | uniq -u > c.txt
Explanation: after cat
and sort
the 2 files together you have the subset lines duplicated, uniq -u
shows only the uniq lines, those are the lines which are only present in one of the file. Duplicating one of the input (a.txt
above) suppresses all the lines exist in that file on the output.
Duplication in any of the files ruins the output of the commands above, if you have duplicates in your files you have to remove those first and run the commands above on the newly created files:
sort a.txt | uniq | aa.txt
sort b.txt | uniq | bb.txt
You can check the result, this 2 command should give you the same checksum:
sort b.txt c.txt | uniq | sha256sum
sort a.txt c.txt | uniq | sha256sum
If one of the file is a superset of the other (so it has all lines of the other plus (maybe) more) then you can simplify a bit. Like in your example the b.txt
is the superset, so this 2 commands should give you the same checksum too:
sort b.txt | sha256sum
sort a.txt c.txt | sha256sum
a.txt
is a subset ofb.txt
. I've already known thata.txt
is a subset ofb.txt
. My question is how to print the difference into a file.