I want to remove all lines except the line(s) containing matching pattern.
This is how I did it:
sed -n \'s/matchingpattern/matchingpattern/p\' file.
sed '/pattern/!d' file.txt
But you're reinventing grep here.
grep