The sed command in Linux is a stream editor that reads text line by line, applies editing commands and prints the result. The most common use is find and replace: sed 's/old/new/g' file.txt replaces every old with new on each line and prints the result without changing the file. Add -i to edit the file in place.
sed never loads the whole file into memory. It reads one line at a time into a pattern space, runs your script on it and writes it out, which is why it works well in pipelines and on very large files. This guide uses GNU sed, the version shipped with most Linux distributions.
The s command has four parts separated by a delimiter, usually a slash.
Create a small file to practise on.
Without a flag, sed replaces only the first match on each line. The original file is unchanged because sed writes to standard output by default.
Flags go after the final delimiter and change how matches are replaced.
| Flag | Meaning | Example |
|---|---|---|
g | Replace every match on the line | s/a/b/g |
2 | Replace only the second match | s/a/b/2 |
2g | Replace the second match and all after it | s/a/b/2g |
I | Match case-insensitively (GNU sed) | s/error/ERR/I |
p | Print the line if a substitution happened | used with -n |
To show only the lines that changed, combine -n (suppress automatic printing) with the p flag.
The delimiter can be any single character. When a pattern contains slashes, such as a file path, pick another delimiter to avoid escaping every slash.
All three print /opt/bin. The | and # forms are far easier to read and are the usual choice for paths and URLs.
By default sed uses basic regular expressions (BRE), where +, ?, (, ) and | need a backslash to act as operators. The -E option switches to extended regular expressions (ERE), which is what most people expect.
Each pair of parentheses is a capture group. In the replacement, \1, \2 and \3 refer to them in order. The special & stands for the entire match.
To put a literal & in the replacement, escape it as \&. To match special characters such as ., * or [ literally, escape them with a backslash.
An address placed before a command restricts which lines it runs on. You can use a line number, a range or a pattern.
Addresses make sed precise: instead of replacing a word everywhere, you replace it only in the section of the file where it belongs.
The -i option writes the result back to the file. Because that is destructive, give it a suffix to keep a backup.
Good habits when using -i:
-i first and check the output.-i.bak (or work in a version-controlled directory) so you can undo a mistake.$ or backticks.-i requires an argument, so sed -i '' 's/a/b/' file is the form there. This guide assumes Linux.You can apply the same edit to many files with a loop or with find and xargs.
Substitution is the headline feature, but a few other commands cover most daily tasks.
Use -e for each command or separate commands with semicolons inside one script. For longer scripts, save them in a file and run sed -f script.sed input.txt.
Without -i, sed prints the edited text to the terminal and leaves the file alone. Redirect the output to a new file or add -i to modify the original. Never redirect to the same file you are reading (sed ... f > f), because the shell empties it first.
Use another delimiter such as | or #: sed 's|/var/log|/srv/log|g'. Alternatively escape each slash with a backslash.
GNU sed supports the I flag: sed 's/error/ERR/Ig'. This flag is a GNU extension and may not exist in other implementations.
sed is built around editing text with patterns and substitutions. awk is built around fields and calculations on columns. For simple replacements sed is shorter; for column data awk is the better tool.
The sed 's/pattern/replacement/flags' command is the standard way to find and replace text on the Linux command line. Add g to replace all matches on a line, -E for readable regular expressions, an address to limit which lines change, and -i.bak to edit a file in place with a backup. Test every command without -i first, then apply it for real.