How to find duplicate lines across 2 different files? Unix

怎甘沉沦 提交于 2019-11-30 17:55:29

As @tjameson mentioned it may be solved in another thread. Just would like to post another solution: sort file1 file2 | awk 'dup[$0]++ == 1'

  1. refer to awk guide to get some awk basics, when the pattern value of a line is true this line will be printed

  2. dup[$0] is a hash table in which each key is each line of the input, the original value is 0 and increments once this line occurs, when it occurs again the value should be 1, so dup[$0]++ == 1 is true. Then this line is printed.

Note that this only works when there are not duplicates in either file, as was specified in the question.

If you want to get a list of repeated lines without resorting to AWK, you can use -d flag to uniq:

sort file1 file2 | uniq -d
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!