sed is a command line editor for POSIX environments. It processes one or more files according to an editing script and writes the results to standard output. Created at Bell Labs, it has been around since the mid-70s.
sed (acronym for stream editor) was created at Bell Labs by Lee McMahon in 1973. It is one of the basic tools in the POSIX environment—it processes one or more files according to an editing script and writes the results to standard output.
Generally, sed is used when a text file needs to be edited programmatically; i.e. without using an interactive text editor (such as ed, vi, nano). For example, this command
sed "s/bar/42/g" file
will print the contents of file
where all occurrences of bar
are replaced by 42
. By default, sed prints to stdout rather than overwriting the input file.
Perhaps notice that the regular expression dialect supported by sed
is very "traditional", and lacks many of the facilities offered by more modern dialects. Generally, sed
does not support greedy matching .*?
or lookarounds (?!...)
, (?<=...)
etc; indeed, plain sed
traditionally does not even support +
for "one or more" repetition, though modern BRE has it in the form \+
(and also \?
for optional, etc), and some sed
implementation support extended regex with a nonstandard option like -E
or -r
, where the backslashes are not required for these constructs. Shorthands like \s
for space, \d
for digits, etc are also not portable, though some sed
implementations do support them.
sed commands consist of an optional address, a single-letter function, and the function's arguments. A script is made up of one or more commands separated by semicolons. Some of the most commonly used functions are:
Function | Arguments | Description |
---|---|---|
s Substitute |
<search pattern>/<replacement text>/<flags> | Performs text replacement |
d Delete |
none | Delete the current line |
a Append |
<text to be appended> | Append text to the current line |
i Insert |
<text to be inserted> | Insert text at the current location |
p |
none | Print the current line |
q Quit |
none | Quit the script |
Popular questions
Some frequently asked Bash questions include:
- sed substitution with Bash variables
- Using different delimiters in sed commands and range addresses
- Combining two sed commands
- How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?
- Non greedy (reluctant) regex matching in sed?
- Is it possible to escape regex metacharacters reliably with sed
- BSD/macOS Sed vs GNU Sed vs the POSIX Sed specification
Resources
External Resources
- GNU sed user's manual
- The sed POSIX specification
- The Sed FAQ
- All Things Sed
- SED & Regular Expressions
- Turing machine simulator (Sed)
- Further Related Questions
- Sed One Liners
- Character Classes and Bracket Expressions
Free Sed Books
- Sed - An Introduction and Tutorial
- GNU sed one-liners - includes a chapter on regexp
Other Stack Exchange sites
- tagged sed on Unix & Linux
- tagged sed on Ask Ubuntu
- tagged sed on SuperUser
- tagged sed on Server Fault