How to make sed remove lines not matched by a substitution
问题 I basically want to do this: cat file | grep '<expression>' | sed 's/<expression>/<replacement>/g' without having to write the expression twice: cat file | sed 's/<expression>/<replacement>/g' Is there a way to tell sed not to print lines that does not match the regular expression in the substitute command? 回答1: Say you have a file which contains text you want to substitute. $ cat new.text A B If you want to change A to a then ideally we do the following - $ sed 's/A/a/' new.text a B But if