Set Operations in Linux

Comments Off on Set Operations in Linux

 

  1. Set Union
    The set union operation unions two sets, i.e., join them into one set. We write C = AB to denote the union of sets A and B which produces set C.

    cat setA setB | sort -u
  2. Set common elements or intersection  (C=A∩B)
    sort setA > setA.sorted 
    sort setB > setB.sorted
    comm -1 -2 setA.sorted setB.sorted
  3. Difference (subtraction) : C=A-B 
    The set A−B consists of elements that are in A but not in B

    comm -2 -3 setA.sorted setB.sorted

    Similarly for The set B-A consists of elements that are in B but not in A

    comm -1 -3 setA.sorted setB.sorted