Set Operations on Lines of Files

Posted on Jun 14, 2015 in Computer Science

Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.

Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.

It is suggested that you use Python instead of shell script to manipulate text files!

  1. Union lines in 2 files.

    comm f1 f2
    

    Or you can use

    cat f1 f2 | sort -u | uniq -u
    
  2. Show symmetric difference

    comm -3 file1 file2
    
  3. Show difference of 1 and 2

    comm -23 file1 file2
    

Note that lines in files must be sorted first if you use the command comm. To sort lines in a file, you can use the following command.

sort file -o file