Comparing two files in linux terminal

后端 未结 10 1191
一生所求
一生所求 2020-12-22 17:31

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\

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-22 17:51

    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.

提交回复
热议问题