# If you'd like to omit non-matching lines from the result; add ';d' to the end of the expression.
sed -E 's/^(\w+) =.*\n(?=((?!\b\1\b)[^!])*(^\1 =|\Z))//gm;t' <<< "a = 10
a * 3
foo = 8
2 - 1
b = 18
c = 10
d = 8
d = 4
cd = 72
d / 6
d + 1
e = 1
e = 2
e + 1
FooBar1 = 0
Fuz__ = 8
Fuz__ / 1
f = 1
f + 1
f = 2
f + 1
g = 1
1 / 5 * 8 + 4"
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