Assuming suitably quoted patterns,
sed -n "/$pat1/ { /$pat2/ d; p; }" file
-n
tells sed
not to print unless explicitly requested.
/$pat1/ { ... }
says on lines matching $pat1, execute the commands in the braces.
/$pat2/ d;
says on lines with $pat2, d
elete, which automatically cycles to the next line of input and ignores any more commands for this line, skipping the p
. If it does not see $pat2, the d
doesn't fire and proceeds to...
p
means print the current line.
My example:
$: grep '#if ' dcos-deploy.sh
#if (( ${isPreProd:-0} ))
#if [[ "$target_mesosphere_cluster" != CDP ]]
$: pat1="^#if "
$: pat2="\\[\\["
$: sed -En "/$pat1/ { /$pat2/ d; p; }" dcos-deploy.sh
#if (( ${isPreProd:-0} ))