Comparing two unsorted lists in linux, listing the

2020-02-16 06:02发布

I have 2 files with a list of numbers (telephone numbers).

I'm looking for a method of listing the numbers in the second file that is not present in the first file.

I've tried the various methods with:

comm (getting some weird sorting errors)
fgrep -v -x -f second-file.txt first-file.txt (unsure of the result, there should be more)

4条回答
一夜七次
2楼-- · 2020-02-16 06:18
cat f1.txt f2.txt | sort |uniq > file3
查看更多
在下西门庆
3楼-- · 2020-02-16 06:28

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

1
2
21
50

f2.txt

1
3
21
50

21 should appear in third column

#WRONG
$ comm <(sort -n f1.txt) <(sort -n f2.txt)   
                1
2
21
        3
        21
                50

#OK
$ comm <(sort f1.txt) <(sort f2.txt)
                1
2
                21
        3
                50
查看更多
聊天终结者
4楼-- · 2020-02-16 06:37

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.

查看更多
来,给爷笑一个
5楼-- · 2020-02-16 06:44
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
查看更多
登录 后发表回答