how to show lines in common (reverse diff)?

前端 未结 7 994
花落未央
花落未央 2020-11-27 10:35

I have a series of text files for which I\'d like to know the lines in common rather than the lines which are different between them. Command line unix or windows is fine.

7条回答
  •  無奈伤痛
    2020-11-27 10:48

    Found this answer on a question listed as a duplicate. I find grep to be more admin-friendly than comm, so if you just want the set of matching lines (useful for comparing CSVs, for instance) simply use

    grep -F -x -f file1 file2
    

    or the simplified fgrep version

    fgrep -xf file1 file2
    

    Plus, you can use file2* to glob and look for lines in common with multiple files, rather than just two.

    Some other handy variations include

    • -n flag to show the line number of each matched line
    • -c to only count the number of lines that match
    • -v to display only the lines in file2 that differ (or use diff).

    Using comm is faster, but that speed comes at the expense of having to sort your files first. It isn't very useful as a 'reverse diff'.

提交回复
热议问题