Comparing two files in linux terminal
There are two files called "a.txt" and "b.txt" both have a list of words. Now I want to check which words are extra in "a.txt" and are not in "b.txt".
I need a efficient algorithm as I need to compare two dictionaries.
Solution 1:
if you have vim installed,try this:
vimdiff file1 file2
or
vim -d file1 file2
you will find it fantastic.
Solution 2:
Sort them and use comm
:
comm -23 <(sort a.txt) <(sort b.txt)
comm
compares (sorted) input files and by default outputs three columns: lines that are unique to a, lines that are unique to b, and lines that are present in both. By specifying -1
, -2
and/or -3
you can suppress the corresponding output. Therefore comm -23 a b
lists only the entries that are unique to a. I use the <(...)
syntax to sort the files on the fly, if they are already sorted you don't need this.
Solution 3:
If you prefer the diff output style from git diff
, you can use it with the --no-index
flag to compare files not in a git repository:
git diff --no-index a.txt b.txt
Using a couple of files with around 200k file name strings in each, I benchmarked (with the built-in time
command) this approach vs some of the other answers here:
git diff --no-index a.txt b.txt
# ~1.2s
comm -23 <(sort a.txt) <(sort b.txt)
# ~0.2s
diff a.txt b.txt
# ~2.6s
sdiff a.txt b.txt
# ~2.7s
vimdiff a.txt b.txt
# ~3.2s
comm
seems to be the fastest by far, while git diff --no-index
appears to be the fastest approach for diff-style output.
Update 2018-03-25 You can actually omit the --no-index
flag unless you are inside a git repository and want to compare untracked files within that repository. From the man pages:
This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.
Solution 4:
Try sdiff
(man sdiff
)
sdiff -s file1 file2