# If you'd like to omit non-matching lines from the result; add ';d' to the end of the expression.
sed -E 's/(\d+)(?=(,|:).*?~.*?~.*?\1_(.))|#|,|:.*/$3/g;t' <<< "#1,0110,0010:~;0101_1000;0110_1001;0111_1010;1000_1011;1001_1100;1010_1101;1011_1110;1100_1111,~0000_00001_10010_20011_30100_40101_50110_60111_71000_81001_9"
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for SED, please visit: https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html