Results 1 to 5 of 5

Thread: sort: union, intersection, and ???

  1. #1
    Join Date
    Dec 2007
    Beans
    12,521

    sort: union, intersection, and ???

    I was reading https://github.com/jlevy/the-art-of-command-line and I found this:
    Code:
          cat a b | sort | uniq > c   # c is a union b
          cat a b | sort | uniq -d > c   # c is a intersect b
          cat a b b | sort | uniq -u > c   # c is set difference a - b
    Could someone please explain the third line?

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sort: union, intersection, and ???

    By itself, and assuming a sorted input, uniq prints out each line once. With the -u option, only those lines that are not repeated are printed. It is the opposite of -d. So cat a b | sort | uniq -u > c would eliminate the lines that are in both set a and b, in other words a XOR b. By repeating b and using -u, it guarantees that anything in b is eliminated from the final list, whether or not it is also in a, in other words a - b.

  3. #3
    Join Date
    Dec 2007
    Beans
    12,521

    Re: sort: union, intersection, and ???

    Quote Originally Posted by Lars Noodén View Post
    By itself, and assuming a sorted input, uniq prints out each line once. With the -u option, only those lines that are not repeated are printed. It is the opposite of -d. So cat a b | sort | uniq -u > c would eliminate the lines that are in both set a and b, in other words a XOR b. By repeating b and using -u, it guarantees that anything in b is eliminated from the final list, whether or not it is also in a, in other words a - b.
    Thank you for explaining

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: sort: union, intersection, and ???

    No problem.

    By the way, I notice that cat is unnecessary there. The utility sort can read files directly:

    Code:
    sort a b | uniq > c
    sort a b | uniq -d > c
    sort a b b | uniq -u > c

  5. #5
    Join Date
    Dec 2007
    Beans
    12,521

    Re: sort: union, intersection, and ???

    Quote Originally Posted by Lars Noodén View Post
    No problem.

    By the way, I notice that cat is unnecessary there. ...
    Then it's another case of UUOC: http://porkmail.org/era/unix/award.html and http://stackoverflow.com/questions/1...ess-use-of-cat

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •