Sed commands
d
: delete text.p
: print text.s
: search and replace txt.
Sed options
-e
: when use many commands.-f
: load the commands in the script-file.-n
: silent mode.
Example
- Print lines containing a pattern:
$ sed -n '/erors/p' example`
- Deleting lines of input containing a pattern
$ sed 'erors/d' example
- The meta-characters mean either starting and ending
# ^ is staring and $ is ending. $ sed -n '/^This.*errors.$/p' example
- Ranges of lines
$ sed -n '2,4p' example $ sed '3,$d' example # $ sed -n '/(From the line pattern matches)/,/(To the line pattern matches)/p' example $ sed -n '/a text/,This/p' example It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. This is a line not containing any errors.
- Find and replace
$ sed 's/erors/errors/g' example
MISC.
- Use the temporary file in script:
TEMPFILE="/var/tmp/sed.$PID.tmp" # $PID, $$ or $BASHPID
Reference
- https://www.tldp.org/LDP/Bash-Beginners-Guide/html/chap_05.html
近期评论