Shell Magic: Set Operations with uniq
blog.deadvax.net
Shell Magic: Set Operations with uniq
1–10 of 67 posts
Re: Shell Magic: Set Operations with uniq
#2https://en.wikipedia.org/wiki/Comm
# show only items in both a and b
comm -1 -2 a_list b_list
# show only items unique to a
comm -2 -3 a_list b_list
# show only items unique to b
comm -1 -3 a_list b_listRe: Shell Magic: Set Operations with uniq
#3Re: Shell Magic: Set Operations with uniq
#4An inefficient solution which involves unnecessary sorting: for each file_i, 0 <= i < n in the set of n files, cat it 2^i times before combining to pipe through sort and uniq -c. Every possible set operation combination can be determined by grepping the result for a particular combination of counts. Intersection would calculated by grepping for 2^n - 1 while symmetric difference would require egrep to pick out any of 1, 2, 4,..,2^(n-1).
Re: Shell Magic: Set Operations with uniq
#5Re: Shell Magic: Set Operations with uniq
#6Re: Shell Magic: Set Operations with uniq
#7This is a neat hack! For two input files, the intersection and relative complement can be done more straightforwardly with comm. https://en.wikipedia.org/wiki/Comm # show only items in both a and b comm -1 -2 a_list b_list # show only items unique to a comm -2 -3 a_list b_list # show only items unique to b comm -1 -3 a_list b_list
comm
To use the output of a command as input.
This also works with diff and other commands.Re: Shell Magic: Set Operations with uniq
#8This is a neat hack! For two input files, the intersection and relative complement can be done more straightforwardly with comm. https://en.wikipedia.org/wiki/Comm # show only items in both a and b comm -1 -2 a_list b_list # show only items unique to a comm -2 -3 a_list b_list # show only items unique to b comm -1 -3 a_list b_list
Re: Shell Magic: Set Operations with uniq
#9http://www.pixelbeat.org/cmdline.html#sets
Note comm output is a bit awkward to parse, so I use another coreutils `join` command to process already sorted data
Re: Shell Magic: Set Operations with uniq
#10The cut and join commands are also useful. I'm pretty sure you could prove all basic database operations can be done using the shell.
It's too disgusting to share.