Set Operations in Unix Shell

50 %
50 %
Information about Set Operations in Unix Shell
How-to & DIY

Published on December 4, 2008

Author: pkrumins

Source: slideshare.net

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!

Set Operations in the Unix Shell v1.01 Created by Peteris Krumins (peter@catonmat.net). Peter's blog: http://www.catonmat.net – good coders code, great reuse Set Membership grep -xc 'element' set grep -xq 'element' set awk '\$0 == quot;elementquot; { 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 Set Intersection cat set1 set2 comm -12 <(sort set1) <(sort set2) awk 1 set1 set2 grep -xF -f set1 set2 cat set1 set2 ... setn sort set1 set2 | uniq -d cat set1 set2 | sort -u join <(sort -n A) <(sort -n B) sort set1 set2 | uniq awk 'NR==FNR { a[\$0]; next } \$0 in a' set1 set2 sort -u set1 set2 awk '!a[\$0]++' Set Complement Set Cardinality comm -23 <(sort set1) <(sort set2) wc -l set | cut -d' ' -f1 grep -vxF -f set2 set1 wc -l < set sort set2 set2 set1 | uniq -u awk 'END { print NR }' set awk 'NR==FNR { a[\$0]; next } !(\$0 in a)' set2 set1 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 quot;\$@quot;) | while read r ; do echo -e quot;\$1 \$rn\$rquot;; done } \$ p `cat set` Set Cartesian Product while read a; do while read b; do echo quot;\$a, \$bquot;; done < set1; done < set2 awk 'NR==FNR { a[\$0]; next } { for (i in a) print i, \$0 }' set1 set2 Disjoint Set Test Empty Set Test comm -12 <(sort set1) <(sort set2) wc -l set | cut -d' ' -f1 awk '++seen[\$0] == 2 { exit 1 }' set1 set2 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 }'

 User name: Comment:

Related presentations

Becoming is Believing - Turn Thought to Action

March 27, 2014

Belief comes through hearing. In order to have belief, we must listen, but we also...

How to organize a community event

March 30, 2014

Presentation held at the Etsy Team Captain Summit in Berlin, March 14-16, 2014

March 30, 2014

CSEduino - Build your Arduino from scratch

March 31, 2014

An how-to build an inexpensive Arduino Board for 5 USD.

How to start payroll software in mid year

March 25, 2014

step by step guide on how small businesses can start payroll software in -mid-year.

small electronics for your makerspace 2 (clc trend...

March 31, 2014

CLC Trendspotting - The Technologies of Makerspaces, presented by Edward Iglesias ...

Related pages

Set Operations in the Unix Shell - good coders code, great ...

Hi Peteris. Nice article, which intersects with something I wrote last year about shell scripting, using the set operations example. It's always good to ...

Set Operations in the Unix Shell - good coders code, great ...

Title: Set Operations in the Unix Shell Author: Peteris Krumins (peter@catonmat.net) Keywords: sh, bash, unix, awk, sets, intersect, union, linux, shell

pkrumins/set-operations-in-unix-shell - GitHub

This is an implementation of 14 set operations by using only Unix utilities. It was created by Peteris Krumins (peter@catonmat.net). His blog is at http ...

GitHub - cdax/setop: Set operations in the UNIX shell!

setop - Set operations in the UNIX shell! ... HTTPS (recommended) Clone with Git or checkout with SVN using ...

Unix - Shell Basic Operators - Tutorials for SAP SD ...

Unix Shell Basic Operators ... A beginner's tutorial containing complete knowledge of Unix Korn and Bourne Shell and ... Checks if file has its set ...

1. Unix and shell commands — unix tutorial 0.1 documentation

1. Unix and shell commands ... Shell commands are particular to the shell (tcsh, in this case). Unix commands are common to ... set is for setting shell ...

UNIX Shell

The Services for UNIX Korn shell provides a set of predefined aliases. For more information, see the Services for UNIX online help for alias. Top Of Page.