Indeed, that works with some tiny fixes:
sed -i -e"/^[[:space:]]*iface[[:space:]]\+$i[[:space:]]\+.*dhcp/,/^[[:space:]]*\(iface\|auto\|mapping\|source\|allow-\)/ { /^[[:space:]]*iface[[:space:]]\+$i[[:space:]]\+.*dhcp/ s/^/#NetworkManager#/; /^[[:space:]]*\(iface\|auto\|mapping\|source\|allow-\)/ ! s/^[^#]/#NetworkManager#/; }" /etc/network/interfaces
(just getting rid of the double #NetworkManager# comments for the iface line; and typos for the /'s.
Indeed, that works with some tiny fixes:
sed -i -e"/^[[ :space: ]]*iface[ [:space: ]]\+$i[ [:space: ]]\+.*dhcp/ ,/^[[:space: ]]*\(iface\ |auto\| mapping\ |source\ |allow- \)/ { /^[[:space: ]]*iface[ [:space: ]]\+$i[ [:space: ]]\+.*dhcp/ s/^/#NetworkMan ager#/; /^[[:space: ]]*\(iface\ |auto\| mapping\ |source\ |allow- \)/ ! s/^[^#] /#NetworkManage r#/; }" /etc/network/ interfaces
(just getting rid of the double #NetworkManager# comments for the iface line; and typos for the /'s.