I want to execute some sed
command for any line that matches either the and
or or
of multiple commands: e.g., sed '50,70/abc/d'
would delete all lines in range 50,70
that match /abc/
, or a way to do sed -e '10,20s/complicated/regex/' -e '30,40s/complicated/regex/
without having to retype s/compicated/regex/
相关问题
- softlinks atime and mtime modification
- UNIX Bash - Removing double quotes from specific s
- Get unexpanded argument from bash command line
- Split a .txt file based on content
- Include and Execute EXE in C# Command Line App
相关文章
- Reverse four length of letters with sed in unix
- Compile and build with single command line Java (L
- How to update command line output?
- Spread 'sed' command over multiple lines
- How to execute another python script from your scr
- Why does sed command contain at symbols
- Python file keyword argument?
- Interactively merge files tracked with git and unt
sed is excellent for simple substitutions on individual lines but for anything else just use awk for clarity, robustness, portability, maintainability, etc...
I don't think
sed
has the facility for multiple selection criteria, my advice would be to step up toawk
, where you can do something like:To delete lines from 10 to 20 and 30 to 40 matching your complicated regex with GNU sed:
or:
bA
: jump to label:A
b
: a jump without label -> jump to end of scriptd
: delete lineLogical-and
The
and
part can be done with braces:Further, braces can be nested for multiple
and
conditions.(The above was tested under GNU
sed
. BSDsed
may differ in small but frustrating details.)Logical-or
The
or
part can be handled with branching: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: