sed command generator
Build a sed command for the common stream-editing jobs: substitute text, delete lines, print a range or insert content. Pick the operation, set the pattern and any line addressing, and decide whether to edit the file in place. The command updates live with every part explained, and a warning appears whenever you enable in-place editing. Assembled entirely in your browser.
What this sed command generator does
sed, the stream editor, transforms text line by line as it flows from input to output. It is the fastest way to substitute strings, delete or print specific lines, and make scripted edits to config files. Its mini-language is terse: a one-letter command, an optional address that selects which lines, and flags. That density makes sed powerful and easy to get wrong, especially the in-place -i flag that rewrites the file. This generator builds the command from plain choices and explains each piece.
The general shape is sed '[address]command' file. The address selects lines (a number, a range like 10,20, or a /regex/); leave it empty to act on every line. The command is the operation: s for substitute, d for delete, p for print, i and a for insert and append. The substitute command takes its own form, s/find/replace/flags, where g replaces every match on a line rather than just the first.
The operations you will use most
| Goal | Command |
|---|---|
| Replace all “foo” with “bar” | sed 's/foo/bar/g' file |
| Replace only on lines 10-20 | sed '10,20s/foo/bar/g' file |
| Delete blank lines | sed '/^$/d' file |
| Delete line 3 | sed '3d' file |
| Print only lines 5-10 | sed -n '5,10p' file |
| Edit the file in place | sed -i 's/foo/bar/g' file |
In-place editing and backups
By default sed prints the result to standard output and leaves the file untouched, which is the safe way to preview a change. Adding -i rewrites the file directly. Because that is irreversible, GNU sed lets you keep a safety copy by attaching a suffix: -i.bak writes the edited file and saves the original as file.bak. Always run the command once without -i to confirm the output, then add the flag. On macOS and BSD, -i requires an explicit suffix argument, even an empty one (-i ''), which is a common portability gotcha.
Regex and delimiters
The find pattern is a basic regular expression by default; add the right escaping or use -E for extended regex. When your pattern or replacement contains slashes, such as a file path, switch the delimiter: sed accepts any character after s, so s|/old/path|/new/path|g avoids escaping every slash. This generator switches the delimiter automatically when it detects a slash in your text, keeping the command readable.
Privacy and how this tool runs
The command is built by JavaScript in your browser. No patterns, replacements or file names are sent anywhere or logged. You can use the generator offline once the page has loaded.
Frequently asked questions
How do I replace text in a file with sed?
Use sed 's/old/new/g' file to print the result, or sed -i 's/old/new/g' file to edit the file in place. The trailing g replaces every occurrence on each line; without it only the first match per line changes.
What does the g flag do?
By default the substitute command replaces only the first match on each line. The g (global) flag makes it replace every match on the line. To replace from the second match onward, combine a number with g, for example s/a/b/2g.
How do I delete lines matching a pattern?
Use the delete command with a pattern address: sed '/pattern/d' file removes every line containing the pattern. sed '/^$/d' removes blank lines, and sed '3d' removes line 3. Add -i to apply the deletion to the file.
How do I edit a file in place safely?
Run the command first without -i to preview the output. When it looks right, add -i.bak so sed edits the file and keeps the original as file.bak. On macOS or BSD sed, the suffix is mandatory; use -i '' for no backup.
How do I print only specific lines?
Combine -n (suppress automatic printing) with the print command and an address: sed -n '5,10p' file prints lines 5 through 10. Without -n every line would print and the matched ones would print twice.
When should I use awk instead of sed?
Use sed for line-oriented substitutions, deletions and simple edits. Reach for awk when you need to work with fields and columns, do arithmetic, or apply conditional logic across records. They complement each other; many pipelines use sed to clean text and awk to extract or compute.
Related tools and resources
More command builders from the same toolkit.













