Comparing two unsorted lists in linux, listing the unique in the second file Comparing two unsorted lists in linux, listing the unique in the second file bash bash

Comparing two unsorted lists in linux, listing the unique in the second file


grep -Fxv -f first-file.txt second-file.txt

Basically looks for all lines in second-file.txt which don't match any line in first-file.txt. Might be slow if the files are large.

Also, once you sort the files (Use sort -n if they are numeric), then comm should also have worked. What error does it give? Try this:

comm -23 second-file-sorted.txt first-file-sorted.txt


You need to use comm:

comm -13 first.txt second.txt

will do the job.

ps. order of first and second file in command line matters.

also you may need to sort files before:

comm -13 <(sort first.txt) <(sort second.txt)

in case files are numerical add -n option to sort.


This should work

comm -13 <(sort file1) <(sort file2)

It seems sort -n (numeric) cannot work with comm, which uses sort (alphanumeric) internally

f1.txt

122150

f2.txt

132150

21 should appear in third column

#WRONG$ comm <(sort -n f1.txt) <(sort -n f2.txt)                   1221        3        21                50#OK$ comm <(sort f1.txt) <(sort f2.txt)                12                21        3                50