Logical-and
The and
part can be done with braces:
sed '50,70{/abc/d;}'
Further, braces can be nested for multiple and
conditions.
(The above was tested under GNU sed
. BSD sed
may differ in small but frustrating details.)
Logical-or
The or
part can be handled with branching:
sed -e '10,20{b cr;}' -e '30,40{b cr;}' -e b -e :cr -e 's/complicated/regex/' file
10,20{b cr;}
For all lines from 10 through 20, we branch to label cr
30,40{b cr;}
For all lines from 30 through 40, we branch to label cr
b
For all other lines, we skip the rest of the commands.
:cr
This marks the label cr
s/complicated/regex/
This performs the substitution on lines which branched to cr
.
With GNU sed
, the syntax for the above can be shortened a bit to:
sed '10,20{b cr}; 30,40{b cr}; b; :cr; s/complicated/regex/' file
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…