Skip to content

Instantly share code, notes, and snippets.

@jtb
Created November 26, 2012 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtb/4151008 to your computer and use it in GitHub Desktop.
Save jtb/4151008 to your computer and use it in GitHub Desktop.
1-way, 2-way, or 3-way Venn Diagram Comparison of Lists
# Written by Justin Brown (jbrown23+gist {at} gmail [dot] com)
# November 26, 2012
if [ $# -lt 1 ] ; then
echo "1-way, 2-way, or 3-way Venn Diagram"
echo "Usage:"
echo " sh venn.sh file1 [file2] [file3]"
echo " Input:"
echo " 1, 2, or 3 lists for comparison"
echo " Output:"
echo " Counts for each section of the Venn diagram"
exit 0
fi
if [ $# -gt 0 ] ; then
TMP1=$(mktemp /tmp/venn.XXXXXX)
sort $1 | uniq > $TMP1
fi
if [ $# -gt 1 ] ; then
TMP2=$(mktemp /tmp/venn.XXXXXX)
sort $2 | uniq > $TMP2
fi
if [ $# -gt 2 ] ; then
TMP3=$(mktemp /tmp/venn.XXXXXX)
sort $3 | uniq > $TMP3
fi
echo "ALL"
if [ $# -lt 2 ] ; then
cat $TMP1 | wc -l #did cat so it won't print out filename
elif [ $# -lt 3 ] ; then
comm -12 $TMP1 $TMP2 | sort | uniq | wc -l
else
comm -12 $TMP1 $TMP2 | sort | uniq | comm -12 - $TMP3 | sort | uniq | wc -l
fi
if [ $# -gt 2 ] ; then
echo "Just $1"
cat $TMP2 $TMP3 | sort | uniq | comm -23 $TMP1 - | wc -l
echo "Just $2"
cat $TMP1 $TMP3 | sort | uniq | comm -23 $TMP2 - | wc -l
echo "Just $3"
cat $TMP1 $TMP2 | sort | uniq | comm -23 $TMP3 - | wc -l
elif [ $# -gt 1 ] ; then
echo "Just $1"
comm -23 $TMP1 $TMP2 | sort | uniq | wc -l
echo "Just $2"
comm -13 $TMP1 $TMP2 | sort | uniq | wc -l
fi
if [ $# -gt 2 ] ; then
echo "$1 and $2, not $3"
comm -12 $TMP1 $TMP2 | sort | uniq |comm -13 $TMP3 - | wc -l
echo "$1 and $3, not $2"
comm -12 $TMP1 $TMP3 | sort | uniq | comm -13 $TMP2 - | wc -l
echo "$2 and $3, not $1"
comm -12 $TMP2 $TMP3 | sort | uniq | comm -13 $TMP1 - | wc -l
fi
trap 'rm -f $TMP1 $TMP2 $TMP3' 0 1 2 3 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment