extracting unique values between 2 sets/files

前端 未结 8 1231
粉色の甜心
粉色の甜心 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:28

    If you are really set on doing this from the command line, this site (search for "no duplicates found") has an awk example that searches for duplicates. It may be a good starting point to look at that.

    However, I'd encourage you to use Perl or Python for this. Basically, the flow of the program would be:

    findUniqueValues(file1, file2){
        contents1 = array of values from file1
        contents2 = array of values from file2
        foreach(value2 in contents2){
            found=false
            foreach(value1 in contents1){
                if (value2 == value1) found=true
            }
            if(!found) print value2
        }
    }
    

    This isn't the most elegant way of doing this, since it has a O(n^2) time complexity, but it will do the job.

提交回复
热议问题