sed - Find and Replace from the Command Line

Learn sed for find-and-replace in files. Fix typos, transform text, and edit without opening an editor.

commandsLast updated 2026-02-24

sed: Find and Replace Without Opening a File

You need to replace "foo" with "bar" in a file. You could open it in an editor, Ctrl+H, type both strings, click replace all, save, close. Or:

sed 's/foo/bar/g' file.txt

Done. No editor, no clicking, no saving. That's sed.

The One Pattern You Need

sed 's/old/new/g' file.txt

That's the whole thing. s means substitute. old is what you're replacing. new is what you're replacing it with. g means "all occurrences" (without it, sed only replaces the first match per line).

The slashes are just delimiters - they separate the parts.

How sed Thinks

sed reads each line, applies your rules, and prints the result. It doesn't modify the original file by default - it just outputs the transformed text.

Want to actually change the file? Add -i:

sed -i 's/old/new/g' file.txt

Now the file itself is modified. Use with care.

Common Patterns

Replace text everywhere:

sed 's/error/warning/g' log.txt

Delete lines containing a pattern:

sed '/DEBUG/d' log.txt

The d command deletes matching lines entirely.

Replace only on matching lines:

sed '/header/s/old/new/g' file.txt

Only runs the substitution on lines containing "header".

Delete blank lines:

sed '/^$/d' file.txt

^$ is regex for "start followed immediately by end" - an empty line. You'd know that if you were keeping up with your Regexgolf.

Case-insensitive replace:

sed 's/error/warning/gi' file.txt

The i flag ignores case.

The Delimiter Trick

Slashes are traditional, but they're annoying when your text contains slashes:

sed 's/\/path\/to\/file/\/new\/path/g'  # ugly escape hell
sed 's|/path/to/file|/new/path|g'       # clean

You can use any character as the delimiter. Pipes |, colons :, and # are popular alternatives.

Real Example: Repurposing a Cover Letter (don't do this)

Switching the name:

sed -i 's/companyA/companyB/g' cover_letter.txt

Capitalizing a name that is also a word:

sed -i 's/Mrs\. will/Mrs. Will/' cover_letter.txt

The Mrs\. makes sure you only replace instances referring to the person, not the verb.

sed vs Other Tools

  • Interactive editing? Use your editor.
  • Just searching? Use grep.
  • Need columns? Use awk.
  • Find and replace? That's sed.

sed shines in scripts and pipelines where you can't open an editor. It's also faster than any GUI for bulk changes.

Quick Reference

| What you want | Command | |---------------|---------| | Replace all | sed 's/old/new/g' file | | Replace in-place | sed -i 's/old/new/g' file | | Delete lines | sed '/pattern/d' file | | Delete blank lines | sed '/^$/d' file | | Case-insensitive | sed 's/old/new/gi' file | | Different delimiter | sed 's|old|new|g' file |

Practice

sed is essential for CTF challenges where you need to transform data formats or clean up output for the next command in a pipeline.


Three slashes and a letter. That's sed. s/this/that/g and you're done.