Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created January 8, 2014 20:48
Show Gist options
  • Save coderofsalvation/8324347 to your computer and use it in GitHub Desktop.
Save coderofsalvation/8324347 to your computer and use it in GitHub Desktop.
prints out the differences of two strings (bash function)
# prints out the differences of two strings
# @param string input1
# @param string input2
function strdiff()
{
[ -z "$1" ] && return 1
[ -z "$2" ] && return 1
set -x
mkfifo $$.fifo1 && mkfifo $$.fifo2
if [ -e "$$.fifo1" ] && [ -e "$$.fifo2" ]; then
_strdiff_var_1st_string=$(printf "%s" "$1" | sed 's: :\n:g')
_strdiff_var_2nd_string=$(printf "%s" "$2" | sed 's: :\n:g')
printf "%s\\n" "$_strdiff_var_1st_string" > "$$.fifo1" &
printf "%s\\n" "$_strdiff_var_2nd_string" > "$$.fifo2" &
_strdiff_var_diff=$(awk 'NR == FNR { A[$0]=1; next } !A[$0]' "$$.fifo1" "$$.fifo2")
rm -rf "$$.fifo1"; rm -rf "$$.fifo2"
printf "%s\\n" "$_strdiff_var_diff"
set +x
else
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment