extracting unique values between 2 sets/files

前端 未结 8 1216
粉色の甜心
粉色の甜心 2020-11-29 19:44

Working in linux/shell env, how can I accomplish the following:

text file 1 contains:

1
2
3
4
5

text file 2 contains:



        
相关标签:
8条回答
  • 2020-11-29 20:42

    Using some lesser-known utilities:

    sort file1 > file1.sorted
    sort file2 > file2.sorted
    comm -1 -3 file1.sorted file2.sorted
    

    This will output duplicates, so if there is 1 3 in file1, but 2 in file2, this will still output 1 3. If this is not what you want, pipe the output from sort through uniq before writing it to a file:

    sort file1 | uniq > file1.sorted
    sort file2 | uniq > file2.sorted
    comm -1 -3 file1.sorted file2.sorted
    

    There are lots of utilities in the GNU coreutils package that allow for all sorts of text manipulations.

    0 讨论(0)
  • 2020-11-29 20:44

    with grep:

    grep -F -x -v -f file_1 file_2 
    
    0 讨论(0)
提交回复
热议问题