(cage meme)

CIS241

System-Level Programming and Utilities

Stream Editor (sed)

Erik Fredericks, frederer@gvsu.edu
Fall 2025

Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers

CIS241 | Fredericks | F25 | 26-sed

sed

Manipulates input (stream or file)

Typically:

  • sed [options] '/pattern/ command' input_file
  • sed [options] 'range command' input_file
CIS241 | Fredericks | F25 | 26-sed

Common options

-E to use extended regex

-f filename to load sed commands from a file

--in-place or -i to modify a file in place

  • --in-place=suffix or -isuffix to create a backup file with suffix extension
CIS241 | Fredericks | F25 | 26-sed

Stream manipulation - Deleting lines:

  • sed '4 d'
  • sed '1,4 d'
  • sed -E '/pattern/ d'

($ is the end of file when using ranges!)

CIS241 | Fredericks | F25 | 26-sed

Stream manipulation - substituting text:

  • sed -E 's/pattern/replace_str'
    • Ex: sed -E 's/ *$//' filename

By default, only replaces first match per line

  • Replace all: sed -E 's/pattern/replace_str/g'
  • Replace second: sed -E 's/pattern/replace_str/2'

Can combine patterns with ranges!

  • sed -E '5,$ s/pattern/replace_str/'
CIS241 | Fredericks | F25 | 26-sed

Stream manipulation - substituting text:

  • sed -E 's/pattern/replace_str'

Can also use parts of matched pattern in replacement!

CIS241 | Fredericks | F25 | 26-sed