Unix command to find lines common in two files

前端 未结 11 1274
忘掉有多难
忘掉有多难 2020-11-27 10:32

I\'m sure I once found a unix command which could print the common lines from two or more files, does anyone know its name? It was much simpler than diff.

11条回答
  •  盖世英雄少女心
    2020-11-27 10:52

    On limited version of Linux (like a QNAP (nas) I was working on):

    • comm did not exist
    • grep -f file1 file2 can cause some problems as said by @ChristopherSchultz and using grep -F -f file1 file2 was really slow (more than 5 minutes - not finished it - over 2-3 seconds with the method below on files over 20MB)

    So here is what I did :

    sort file1 > file1.sorted
    sort file2 > file2.sorted
    
    diff file1.sorted file2.sorted | grep "<" | sed 's/^< *//' > files.diff
    diff file1.sorted files.diff | grep "<" | sed 's/^< *//' > files.same.sorted
    

    If files.same.sorted shall have been in same order than the original ones, than add this line for same order than file1 :

    awk 'FNR==NR {a[$0]=$0; next}; $0 in a {print a[$0]}' files.same.sorted file1 > files.same
    

    or, for same order than file2 :

    awk 'FNR==NR {a[$0]=$0; next}; $0 in a {print a[$0]}' files.same.sorted file2 > files.same
    

提交回复
热议问题