Solution using single sed execution:
echo -e "$s1\n$s2" | sed -e 'N;s/^/\n/;:begin;s/\n\(.\)\(.*\)\n\(.*\)\1\(.*\)/\1\n\2\n\3\4/;t begin;s/\n.\(.*\)\n\(.*\)/\n\1\n\2/;t begin;s/\n\n.*//'
Like all cryptic sed scripts, this requires an explanation in the form of a sed script file that can be run echo -e "$s1\n$s2" | sed -f script echo -e "$s1\n$s2" | sed -f script :
If you want to remove the duplicate, add the following at the end of the script:
:end;s/\(.\)\(.*\)\1/\1\2/;t end
Edit: I understand that the solution for a clean dogbane shell has the same algorithm and is probably more efficient.
jfg956 Aug 04 2018-11-11T00: 00Z
source share