how to show lines in common (reverse diff)?

前端 未结 7 1004
花落未央
花落未央 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:54

    I just learned the comm command from this thread, but wanted to add something extra: if the files are not sorted, and you don't want to touch the original files, you can pipe the outptut of the sort command. This leaves the original files intact. Works in bash, I can't say about other shells.

    comm -1 -2 <(sort file1) <(sort file2)
    

    This can be extended to compare command output, instead of files:

    comm -1 -2 <(ls /dir1 | sort) <(ls /dir2 | sort)
    

提交回复
热议问题