Skip to content

Commit 921543f

Browse files
committed
Shell: Add set operations on lines (union, intersection, difference, symdiff, is-subset)
1 parent cf34517 commit 921543f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

home/dot_config/zsh/exact_private_zsh.d/private_010_functions_text.zsh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,30 @@ function echoe()
145145
{
146146
echo "$@" 1>&2
147147
}
148+
149+
##############################################################################
150+
# Set operations on lines
151+
#
152+
# Each function takes two files (or process substitutions) as arguments.
153+
# Input is sorted and deduplicated automatically.
154+
#
155+
# Usage: set-difference <(cmd1) <(cmd2)
156+
#
157+
# @tags: canbescript filter
158+
159+
# A ∪ B — all lines from either input, deduplicated
160+
function set-union() { sort -u "$1" "$2"; }
161+
162+
# A ∩ B — only lines present in both inputs
163+
function set-intersection() { comm -12 <(sort -u "$1") <(sort -u "$2"); }
164+
165+
# A \ B — lines in the first input but not in the second
166+
function set-difference() { comm -23 <(sort -u "$1") <(sort -u "$2"); }
167+
168+
# A △ B — lines in either input but not in both (symmetric difference)
169+
# Not using comm -3 directly because it prepends a tab to one column's output.
170+
function set-symdiff() { { set-difference "$1" "$2"; set-difference "$2" "$1"; } | sort; }
171+
172+
# A ⊆ B — true (exit 0) if all lines in the first input are also in the second
173+
function set-is-subset() { [[ -z $(set-difference "$1" "$2") ]]; }
174+
##############################################################################

home/exact_private_dot_bash.d/private_010_functions_text.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,32 @@ function echoe()
147147
echo "$@" 1>&2
148148
}
149149

150+
##############################################################################
151+
# Set operations on lines
152+
#
153+
# Each function takes two files (or process substitutions) as arguments.
154+
# Input is sorted and deduplicated automatically.
155+
#
156+
# Usage: set-difference <(cmd1) <(cmd2)
157+
#
158+
# @tags: canbescript filter
159+
160+
# A ∪ B — all lines from either input, deduplicated
161+
function set-union() { sort -u "$1" "$2"; }
162+
163+
# A ∩ B — only lines present in both inputs
164+
function set-intersection() { comm -12 <(sort -u "$1") <(sort -u "$2"); }
165+
166+
# A \ B — lines in the first input but not in the second
167+
function set-difference() { comm -23 <(sort -u "$1") <(sort -u "$2"); }
168+
169+
# A △ B — lines in either input but not in both (symmetric difference)
170+
# Not using comm -3 directly because it prepends a tab to one column's output.
171+
function set-symdiff() { { set-difference "$1" "$2"; set-difference "$2" "$1"; } | sort; }
172+
173+
# A ⊆ B — true (exit 0) if all lines in the first input are also in the second
174+
function set-is-subset() { [[ -z $(set-difference "$1" "$2") ]]; }
175+
##############################################################################
176+
150177
# vim: ft=sh fdm=marker expandtab ts=3 sw=3 :
151178

0 commit comments

Comments
 (0)