sed

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

  1. Print lines containing a pattern:
     $ sed -n '/erors/p' example`
    
  2. Deleting lines of input containing a pattern
     $ sed 'erors/d' example
    
  3. The meta-characters mean either starting and ending
     # ^ is staring and $ is ending.
     $ sed -n '/^This.*errors.$/p' example
    
  4. 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.
    
  5. Find and replace
     $ sed 's/erors/errors/g' example
    

MISC.

  1. 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