Set Operations in Unix Shell

Description

Created at http://www.catonmat.net


This document implements 14 set operations by using common Unix utilities such as sort, uniq, diff,

comm, cat, head, tail, awk, and others. It implements the following operations: Set Membership. Set

Equality. Set Cardinality. Subset Test. Set Union. Set Intersection. Set Complement. Set Symmetric

Difference. Power Set. Set Cartesian Product. Disjoint Set Test. Empty Set Test. Minimum. Maximum.


Visit http://www.catonmat.net for full explanation of these operations!

Reviews
Shared by: Peteris Krumins
Stats
views:
147
rating:
not rated
reviews:
0
posted:
12/3/2008
language:
English
pages:
0
Set Operations in the Unix Shell Created by Peteris Krumins (peter@catonmat.net). Peter's blog: http://www.catonmat.net – good coders code, great reuse v1.01 Set Membership grep -xc 'element' set grep -xq 'element' set awk '$0 == "element" { s=1; exit } END { exit !s }' set awk -v e='element' '$0 == e { s=1; exit } END { exit !s }' set Set Equality diff -q <(sort set1) <(sort set2) diff -q <(sort set1 | uniq) <(sort set2 | uniq) awk '{ if (!($0 in a)) c++; a[$0] } END{ exit !(c==NR/2) }' set1 set2 awk '{ a[$0] } END{ exit !(length(a)==NR/2) }' set1 set2 Subset Test comm -23 <(sort subset | uniq) <(sort set | uniq) | head -1 awk 'NR==FNR { a[$0]; next } { if !($0 in a) exit 1 }' set subset Set Union cat set1 set2 awk 1 set1 set2 cat set1 set2 ... setn cat set1 set2 | sort -u sort set1 set2 | uniq sort -u set1 set2 awk '!a[$0]++' Set Intersection comm -12 <(sort set1) <(sort set2) grep -xF -f set1 set2 sort set1 set2 | uniq -d join <(sort -n A) <(sort -n B) awk 'NR==FNR { a[$0]; next } $0 in a' set1 set2 Set Complement comm -23 <(sort set1) <(sort set2) grep -vxF -f set2 set1 sort set2 set2 set1 | uniq -u awk 'NR==FNR { a[$0]; next } !($0 in a)' set2 set1 Set Cardinality wc -l set | cut -d' ' -f1 wc -l < set awk 'END { print NR }' set Set Symmetric Difference comm -3 <(sort set1) <(sort set2) | sed 's/\t//g' comm -3 <(sort set1) <(sort set2) | tr -d '\t' sort set1 set2 | uniq -u cat <(grep -vxF -f set1 set2) <(grep -vxF -f set2 set1) grep -vxF -f set1 set2; grep -vxF -f set2 set1 awk 'NR==FNR { a[$0]; next } $0 in a { delete a[$0]; next } 1; END { for (b in a) print b }' set1 set2 Power Set $ p() { [ $# -eq 0 ] && echo || (shift; p "$@") | while read r ; do echo -e "$1 $r\n$r"; done } $ p `cat set` Set Cartesian Product while read a; do while read b; do echo "$a, $b"; done < set1; done < set2 awk 'NR==FNR { a[$0]; next } { for (i in a) print i, $0 }' set1 set2 Disjoint Set Test comm -12 <(sort set1) <(sort set2) awk '++seen[$0] == 2 { exit 1 }' set1 set2 Empty Set Test wc -l set | cut -d' ' -f1 wc -l < set awk '{ exit 1 }' set Minimum head -1 <(sort set) awk 'NR == 1 { min = $0 } $0 < min { min = $0 } END { print min }' Maximum tail -1 <(sort set) awk 'NR == 1 { max = $0 } $0 > max { max = $0 } END { print max }'

Related docs
Unix Programming Tools
Views: 86  |  Downloads: 19
UNIX
Views: 235  |  Downloads: 68
Unix-and-Shell-Programming---Welcome-To-SSEC
Views: 2  |  Downloads: 0
UNIX Commands
Views: 619  |  Downloads: 107
Introduction to UNIX
Views: 270  |  Downloads: 61
UNIX_shell_script_standards_for_PowerCenter_7
Views: 1545  |  Downloads: 129
Computer Notes
Views: 2  |  Downloads: 0
UNIX C Shell Cheat Sheet
Views: 90  |  Downloads: 17
Unix Tutorial
Views: 99  |  Downloads: 5
UNIX Linux Shell Cheat Sheet
Views: 14  |  Downloads: 5
premium docs
Other docs by Peteris Krumin...
Bash Shell History Editing Cheat Sheet
Views: 937  |  Downloads: 30
Perl Pack Unpack Printf Sprintf Cheat Sheet Summary
Views: 1882  |  Downloads: 20
Ed, The Original UNIX Text Editor, Cheat Sheet
Views: 432  |  Downloads: 35
Sed, UNIX Stream Editor, Cheat Sheet
Views: 772  |  Downloads: 55
Bash Emacs Editing Mode (readline) Cheat Sheet
Views: 467  |  Downloads: 25
Screen VT100ANSI Terminal Emulator Cheat Sheet
Views: 519  |  Downloads: 14
Bash VI Editing Mode (readline) Cheat Sheet
Views: 8408  |  Downloads: 109