Get lines containing PAT1 OR PAT2
of a file
(using PAT1='dog', PAT2='cat' in the example).
2000.11.17
Raw Input
1 pig dolphin
2 bird dog apple
3 dog orange cat
4 mouse juice coffee
5 cow milk cat
Desired Output
2 bird dog apple
3 dog orange cat
5 cow milk cat
Script and Comments
Script1
[ 1] /PAT1/b
[ 2] /PAT2/!d
Comments
When meets a line containing PAT1, sed will execute command 'b'. Since no label is specified, it will go to the end of the script, print this line, and start a new cycle.
When meets a line NOT containing PAT1, sed will proceed to step [2]. If the line does NOT contain PAT2, it will be discarded; otherwise, since the end of script is reached, sed will print the line and start a new cycle.