To replace “blue” with “green” in lines that contain “red”:
:g/red/s/blue/green
To do the replacement in lines that do not contain “red”:
:g!/red/s/blue/green
To replace “blue” with “green” in lines that contain “red”:
:g/red/s/blue/green
To do the replacement in lines that do not contain “red”:
:g!/red/s/blue/green