Sed Command Generator

Build sed commands for find and replace, deleting lines, printing ranges, and inserting text, without memorising the syntax. Preview the result on sample text before you run it.

Updated
Runs in your browser. Your data is not uploaded.
sed command

        
Preview

            

How to use the Sed Command Generator

  1. On the Build tab, pick the Operation: Find and replace, Delete lines, Print lines, Insert before, Append after, or Replace line.
  2. Fill in the fields for that operation and choose which Lines it applies to. Set Edit file in place, Extended regex, and the File name.
  3. Copy the command, and check the Preview of what it does to the sample text.

How it works

A sed command is sed [options] 'script' file. The script is an optional address (which lines) followed by a command (what to do).

  • Find and replace writes s/find/replace/flags. g replaces every match on a line, I ignores case (GNU sed), and a number replaces only that occurrence.
  • If your text contains /, the generator switches to another delimiter such as | or #, so paths don't need escaping.
  • With Literal text on, characters that mean something in a regex (. * [ ] ^ $ \) are escaped, and & is escaped in the replacement.
  • Addresses: 5 is line 5, 10,20 is a range, $ is the last line, and /error/ is every line that matches.
  • The script is wrapped in single quotes for the shell, and any single quote inside it is written as '\''.

Examples

  • Replace every http:// with https:// in links.txt: sed 's|http://|https://|g' links.txt
  • Delete blank lines: sed '/^$/d' file.txt
  • Print lines 10 to 20 only: sed -n '10,20p' file.txt
  • Remove lines containing DEBUG and save the file: sed -i '/DEBUG/d' app.log on Linux, or sed -i '' '/DEBUG/d' app.log on macOS.
  • Add a line after every line that matches [mysqld]: sed '/\[mysqld\]/a max_connections=500' my.cnf

Limitations

  • The preview uses JavaScript regular expressions to imitate sed. It matches sed for everyday patterns, but POSIX character classes and back-references in patterns may behave slightly differently.
  • Multi-line scripts with hold space commands (h, G, x) are not built here. Use the Validate tab to check them.
  • a, i, and c with text on the same line are GNU sed syntax. On macOS, install GNU sed (gsed) or add a backslash and a line break.

Frequently asked questions

Why does sed -i fail on macOS?

BSD sed on macOS needs a backup suffix after -i. Use sed -i '' 's/a/b/' file for no backup. The generator writes this form when you choose macOS.

How do I replace text that contains slashes?

Use a different delimiter: s|/usr/local|/opt|g. The generator does this automatically.

What does the g flag do?

Without g, sed replaces only the first match on each line. With it, every match on the line is replaced.

Is my text uploaded for the preview?

No. The preview runs in your browser.

Often used together with the Sed Command Generator.

  • Regex Tester

    Highlights regex matches live, lists capture groups and offsets, and previews replacements.

  • Diff Checker

    Compares two texts or code files and highlights added, removed, and changed lines.