Sed
Sed
Sed
#sed
Table of Contents
About 1
Remarks 2
References 2
Versions 2
Examples 2
Hello World 2
Syntax 3
Remarks 3
Examples 3
'l' Line-Wrapping 4
Introduction 5
Examples 5
Specific line 5
Examples 10
Examples 11
Introduction 12
Examples 12
Do multiple line regexp replacing with unconditional branch 12
Chapter 7: BSD/macOS Sed vs. GNU Sed vs. the POSIX Sed specification 13
Introduction 13
Remarks 13
Examples 17
Examples 19
Syntax 20
Parameters 20
Remarks 20
Examples 21
Portable Use 21
Examples 24
Examples 25
Backreference 25
Credits 28
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: sed
It is an unofficial and free sed ebook created for educational purposes. All the content is extracted
from Stack Overflow Documentation, which is written by many hardworking individuals at Stack
Overflow. It is neither affiliated with Stack Overflow nor official sed.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ 1
Chapter 1: Getting started with sed
Remarks
References
• FreeBSD sed man-page
• NetBSD sed man-page
• OpenBSD sed man-page
• Illumos sed man-page
• macOS (OS X) man-page
• Plan9 sed man-page
• GNU sed online manual
Versions
BSD sed 1992 FreeBSD 10.3 / NetBSD 7.0 / OpenBSD 5.9 2016-04-04
Examples
Hello World
One of the most common use of Sed is text substitution that can be achieved with the s command.
In a terminal, type echo "Hello sed" | sed 's/sed/World/' and press Enter:
The string "Hello, sed" is sent via pipe as input to the sed command that replace the word sed with
World.
The syntax of a basic substitution command is s followed by the string or pattern to be searched
and the substitution text. s command and strings are separated with a default / delimiter.
https://riptutorial.com/ 2
Chapter 2: Additional Options
Syntax
• -a - (BSD sed) Create / Truncate all files written to before processing
• -E | -r - Use Extended Regular Expressions
• -i | -I - Refer to the topic on In-Place Editing
• -l - (BSD sed) Use line-buffered output
• -l length - (GNU sed) Specify the length for l command line-wrapping
• -s - (GNU sed) Treat files as separate streams
• -u - Do not buffer the output
• -z - (GNU sed) Use the NUL character to separate records
• --quiet | --silent - (GNU sed) Synonyms for -n
• --expression=command - (GNU sed) Synonym for -e
• --file=command_file - (GNU sed) Synonym for -f
• --follow-symlinks - (GNU sed) Follow symlinks
• --in-place[=extension] - (GNU sed) Synonym for -i
• --line-length=length - (GNU sed) Synonym for -l
• --separate - (GNU sed) Synonym for -s
• --unbuffered - (GNU sed) Synonym for -u
• --null-data - (GNU sed) Synonym for -z
• --help - (GNU sed) Print usage
• --version - (GNU sed) Print version
Remarks
The -E option is to be standardized in the next major version, see the relevant issue.
Examples
Delay Creation/Truncation of Files
Files written to with the w command are created/truncated before any commands are run.
Each wfile shall be created before processing begins. Implementations shall support at
least ten wfile arguments in the script; the actual number (greater than or equal to 10)
that is supported by the implementation is unspecified. The use of the wfile parameter
shall cause that file to be initially created, if it does not exist, or shall replace the
contents of an existing file.
https://riptutorial.com/ 3
BSD sed provides the -a option to delay creating/truncating files until they are written to with the w
command.
'l' Line-Wrapping
Long lines shall be folded, with the point of folding indicated by writing a followed by a ;
the length at which folding occurs is unspecified, but should be appropriate for the
output device.
GNU sed provides the -l option to specify the length at which to split long lines when printing with
the l command, defaulting to seventy characters.
BSD sed splits long lines at the number provided by the environment variable COLUMNS, if COLUMNS is
not provided then it splits at the terminal width, and if COULMNS is not provided and the output is not
a terminal then it defaults to sixty characters.
https://riptutorial.com/ 4
Chapter 3: Address and address range
Introduction
Sed commands can be specified to act only on certain lines by using addresses or address ranges
.
Examples
Specific line
$ cat ip.txt
address
range
substitution
pattern
sample
• Nth line
• Last line
$ cat ip.txt
address
range
substitution
pattern
sample
https://riptutorial.com/ 5
pattern
• $ can be used to specify last line. Space can be used between address and command for
clarity
GNU sed
$ cat ip.txt
address
range
substitution
pattern
sample
Add Sub Mul Div
https://riptutorial.com/ 6
• Range of patterns
Note
• In the second example, it matched two ranges - lines 1,2 and lines 4,5
• See Using different delimiters on how to use other characters instead of / for specifying the
pattern
GNU sed
• Case-insensitive match
$ cat ip.txt
address
range
substitution
pattern
sample
Add Sub Mul Div
https://riptutorial.com/ 7
• Line matching pattern to line number
GNU sed
• 0 can be used as starting line number to signal end of range when pattern matches first line
of input
$ cat ip.txt
address
range
substitution
1234
search pattern
sample
Add Sub Mul Div
https://riptutorial.com/ 8
$ sed -n '$p' ip.txt
Add Sub Mul Div
https://riptutorial.com/ 9
Chapter 4: Advanced sed commands
Examples
Insert a new line before matching pattern - using eXchange
line 1
line 2
line 3
line 1
line 2
line 3
Explanation:
xcommand is eXchange. sed has a buffer that you can use to store some lines. This command
exchanges this buffer with current line (so current line goes to this buffer and buffer content
becomes current line).
https://riptutorial.com/ 10
Chapter 5: Append command
Examples
Insert line after first match
line 1
line 2
line 3
You can add a new line after first matching line with the a command.
For portable use the a command must be followed immediately by an escaped newline, with the
text-to-append on its own line or lines.
sed '
/line 2/a\
new line 2.2
' file.txt
GNU sed
Some versions of sed allow the text-to-append to be inline with the a command:
line 1
line 2
new line 2.2
line 3
https://riptutorial.com/ 11
Chapter 6: Branching Operation
Introduction
The branching operation of sed can help control the flow of the program.
Examples
Do multiple line regexp replacing with unconditional branch
$ cat in.txt
a
b
a
c
a
d
I only want to replace the a\nc with deleted, but not a\nb or a\nd.
a
b
"deleted" # see! succeed
a
d
https://riptutorial.com/ 12
Chapter 7: BSD/macOS Sed vs. GNU Sed vs.
the POSIX Sed specification
Introduction
To quote from @SnoringFrog's topic-creation request:
"One of the biggest gotchas using sed is scripts that fail (or succeed in an unexpected way)
because they were written for one and not the other. Simple run-down of the more major
differences would be good."
Remarks
macOS uses the BSD version of sed[1], which differs in many respects from the GNU sed
version that comes with Linux distros.
Their common denominator is the functionality decreed by POSIX: see the POSIX sed spec.
The most portable approach is to use POSIX features only, which, however, limits
functionality:
• Notably, POSIX specifies support only for basic regular expressions, which have many
limitations (e.g., no support for | (alternation) at all, no direct support for + and ?) and
different escaping requirements.
○ Caveat: GNU sed (without -r), does support \|, \+ and \?, which is NOT POSIX-
compliant; use --posix to disable (see below).
○ (both versions): use only the -n and -e options (notably, do not use -E or -r to turn on
support for extended regular expressions)
○ GNU sed: add option --posix to ensure POSIX-only functionality (you don't strictly need
this, but without it you could end up inadvertently using non-POSIX features without
noticing; caveat: --posix itself is not POSIX-compliant)
https://riptutorial.com/ 13
• what extensions they implement differs (GNU sed implements more).
• even those extensions they both implement partially differ in syntax.
• Incompatible features:
○ -i sensibly turns on per-input-file line numbering in GNU sed and recent versions
of BSD sed (e.g., on FreeBSD 10), but does NOT on macOS as of 10.12.
Note that in the absence of -i all versions number lines cumulatively across input files.
○ If the last input line does not have a trailing newline (and is printed):
○ BSD sed: always appends a newline on output, even if the input line doesn't end
in one.
○ GNU sed: preserves the trailing-newline status, i.e., it appends a newline only if
the input line ended in one.
• Common features:
○ If you restrict your sed scripts to what BSD sed supports, they will generally work in
GNU sed too - with the notable exception of using platform-specific extended regex
features with -E. Obviously, you'll also forgo extensions that are specific to the GNU
version. See next section.
Guidelines for cross-platform support (OS X/BSD, Linux), driven by the stricter
requirements of the BSD version:
Note that that the shorthands macOS and Linux are occasionally used below to refer to the BSD and GNU versions of
sed, respectively, because they are the stock versions on each platform. However, it is possible to install GNU sed on
macOS, for instance, using Homebrew with brew install gnu-sed.
Note: Except when the -r and -E flags are used (extended regexes), the instructions below
amount to writing POSIX-compliant sed scripts.
• For POSIX compliance, you must restrict yourself to POSIX BREs (basic regular
expressions), which are, unfortunately, as the name suggests, quite basic.
Caveat: do not assume that \|, \+ and \? are supported: While GNU sed supports them
(unless --posix is used), BSD sed does not - these features are not POSIX-compliant.
While \+ and \? can be emulated in POSIX-compliant fashion :
\{1,\} for \+,
\{0,1\} for \?,
\| (alternation) cannot, unfortunately.
https://riptutorial.com/ 14
• For more powerful regular expressions, use -E (rather than -r) to support EREs (extended
regular expressions) (GNU sed doesn't document -E, but it does work there as an alias of -r
; newer version of BSD sed, such as on FreeBSD 10, now also support -r, but the macOS
version as of 10.12 does not).
Caveat: Even though use of -r / -E means that your command is by definition not POSIX-
compliant, you must still restrict yourself to POSIX EREs (extended regular expressions)
. Sadly, this means that you won't be able to use several useful constructs, notably:
○ In regexes (both in patterns for line selection and the first argument to the s function),
assume that only \n is recognized as an escape sequence (rarely used, since the
pattern space is usually a single line (without terminating \n), but not inside a character
class, so that, e.g., [^\n] doesn't work; (if your input contains no control chars. other
than \t, you can emulate [^\n] with [[:print:][:blank:]]; otherwise, splice control
chars. in as literals[2]) - generally, include control characters as literals, either via
spliced-in ANSI C-quoted strings (e.g., $'\t') in shells that support it (bash,ksh,
zsh), or via command substitutions using printf (e.g., "$(printf '\t')").
○ Linux only:
sed 's/\t/-/' <<<$'a\tb' # -> 'a-b'
○ OSX and Linux:
sed 's/'$'\t''/-/' <<<$'a\tb' # ANSI C-quoted string
sed 's/'"$(printf '\t')"'/-/' <<<$'a\tb' # command subst. with printf
○ Linux only:
sed 's/-/\t/' <<<$'a-b' # -> 'a<tab>b'
○ macOS and Linux:
sed 's/-/'$'\t''/' <<<'a-b'
sed 's/-/'"$(printf '\t')"'/' <<<'a-b'
○ Ditto for the text arguments to the i and a functions: do not use control-character
sequences - see below.
• Labels and branching: labels as well as the label-name argument to the b and t functions
must be followed by either by a literal newline or a spliced-in $'\n'. Alternatively, use
multiple -e options and terminate each right after the label name.
○ Linux only:
sed -n '/a/ bLBL; d; :LBL p' <<<$'a\nb' # -> 'a'
https://riptutorial.com/ 15
○ macOS and Linux:
○EITHER (actual newlines):
sed -n '/a/ bLBL d; :LBL p' <<<$'a\nb'
○ OR (spliced-in $\n instances):
sed -n '/a/ bLBL'$'\n''d; :LBL'$'\n''p' <<<$'a\nb'
○ OR (multiple -e options):
sed -n -e '/a/ bLBL' -e 'd; :LBL' -e 'p' <<<$'a\nb'
• Functions i and a for inserting/appending text: follow the function name by \, followed
either by a literal newline or a spliced-in $'\n' before specifying the text argument.
○ Linux only:
sed '1 i new first line' <<<$'a\nb' # -> 'new first line<nl>a<nl>b'
○ OSX and Linux:
sed -e '1 i\'$'\n''new first line' <<<$'a\nb'
○ Note:
○ Without -e, the text argument is inexplicably not newline-terminated on output on
macOS (bug?).
○ Do not use control-character escapes such as \n and \t in the text argument,
as they're only supported on Linux.
○ If the text argument therefore has actual interior newlines, \-escape them.
○ If you want to place additional commands after the text argument, you must
terminate it with an (unescaped) newline (whether literal or spliced in), or
continue with a separate -e option (this is a general requirement that applies to all
versions).
• Inside function lists (multiple function calls enclosed in {...}), be sure to also terminate
the last function, before the closing }, with ;.
○ Linux only:
○ sed -n '1 {p;q}' <<<$'a\nb' # -> 'a'
○ macOS and Linux:
○ sed -n '1 {p;q;}' <<<$'a\nb'
GNU features you'll miss out on if you need to support both platforms:
• Various regex-matching and substitution options (both in patterns for line selection and
the first argument to the s function):
○ The I option for case-INsensitive regex matching (incredibly, BSD sed doesn't
support this at all).
○ The M option for multi-line matching (where ^ / $ match the start / end of each line)
○ For additional options that are specific to the s function, see
https://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command
• Escape sequences
https://riptutorial.com/ 16
s/// function that allow substring manipulation, within limits; e.g., sed 's/^./\u&/'
<<<'dog' # -> 'Dog' - see http://www.gnu.org/software/sed/manual/sed.html#The-
_0022s_0022-Command
• Address extensions, such as first~step to match every step-th line, addr, +N to match N
lines following addr, ... - see http://www.gnu.org/software/sed/manual/sed.html#Addresses
[1] The macOS sed version is older than the version on other BSD-like systems such as FreeBSD and PC-BSD.
Unfortunately, this means that you cannot assume that features that work in FreeBSD, for instance, will work [the
same] on macOS.
'[[:print:]'$'\001\002\003\004\005\006\007\010\011\013\014\015\016\017\020\021\022\023\024\025\026\027\0
Examples
Replace all newlines with tabs
Note: For brevity, the commands use here-strings (<<<) and ANSI C-quoted strings ($'...'). Both these shell
features work in bash, ksh, and zsh.
# GNU Sed
$ sed ':a;$!{N;ba}; s/\n/\t/g' <<<$'line_1\nline_2\nline_3'
line_1 line_2 line_3
• Note the need to terminate labels (:a) and branching commands (ba) either with actual
newlines or with separate -e options.
https://riptutorial.com/ 17
Append literal text to a line with function 'a'
Note: For brevity, the commands use here-strings (<<<) and ANSI C-quoted strings ($'...'). Both these shell
features work in bash, ksh, and zsh.
# GNU Sed
$ sed '1 a appended text' <<<'line 1'
line 1
appended text
Note how BSD Seed requires a \ followed by an actual newline to pass the text to append.
The same applies to the related i (insert) and c (delete and insert) functions.
Read BSD/macOS Sed vs. GNU Sed vs. the POSIX Sed specification online:
https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification
https://riptutorial.com/ 18
Chapter 8: Delete command
Examples
Delete one line containing a pattern
line 1
line 2
line 3
You can delete a line from file content with the d command.
The pattern to match is surrounded with default / delimiter and the d command follows the pattern:
line 1
line 3
https://riptutorial.com/ 19
Chapter 9: In-Place Editing
Syntax
• sed -I extension - FreeBSD sed (continuous line-counter)
• sed -I[extension] - NetBSD and Illumos sed (continuous line-counter)
• sed -i extension - FreeBSD sed
• sed -i[extension] - NetBSD, OpenBSD, Illumos, BusyBox and GNU sed
• sed --in-place[=extension] - Illumos, BusyBox, and GNU sed
Parameters
Parameter Details
Save a backup file with the specified extension, or no backup file when
extension
extension is a zero-length string.
Remarks
In-place editing is a common but non-standard extension present in the majority of recent
systems.
(a section like this appears in all current BSD sed manuals, and those of their derivatives)
It is not recommended to give a zero length extension when in place editing files, as it
risks corruption or partial content in situations where disk space is exhausted, etc.
There is definitely a use for sed and for in-place editing features of sed, but when the UNIX
standard is extended, we should always ask why the old UNIX standard did not include that
feature. Though UNIX is not perfect, the orthogonality and completeness of the tools has been
developed to be quite near to perfection, at least for purposes that where visible around 1970:
Text editing and automated text editing was surely visible around that time.
Actually, the idea of sed is not to edit a file in place, but to edit a stream. That's why the name sed
is a short form of stream editor. Take away the s, and you get the tool that was actually designed
for file editing: ed:
https://riptutorial.com/ 20
or cat file_edit_commands | ed file.
Examples
Replacing strings in a file in-place
We use -i to select in-place editing on the $file file. In some systems it is required to add suffix
after -i flag which will be used to create backup of original file. You can add empty string like -i ''
to omit the backup creation. Look at Remarks in this topic about -i option.
$ cat example
one
two
three
total
$ sed -i s/"t"/"g"/g example
$ cat example
one
gwo
ghree
gogal
Portable Use
In-place editing, while common, is a non-standard feature. A viable alternative would be to use an
intermediate file to either store the original, or the output.
To use the -i option with both the GNU and FreeBSD syntax an extension must be specified and
appended to the -i option. The following will be accepted by both, and produce two files, the
original version at file.orig and the edited version at file:
$ cat file
one
two
three
$ sed -i.orig 's/one/XX/' file
$ cat file # the original file has changed its content
XX
https://riptutorial.com/ 21
two
three
$ cat file.orig # the original content is now in file.orig
one
two
three
sed -i -e cmd file will modify file even if its permissions are set to read-only.
rather than
sed -e cmd file > tmp; cat tmp > file; rm tmp
https://riptutorial.com/ 22
Extremely destroyed data (#see, data changed)
This can be mitigated by creating a backup by specifying a SUFFIX with the i option:
https://riptutorial.com/ 23
Chapter 10: Regular expressions
Examples
Using different delimiters
$ cat file
hello/how/are/you
i am fine
If the pattern contains slashes itself, you can use another delimiter using \cBREc:
https://riptutorial.com/ 24
Chapter 11: Substitution
Examples
Substitution Using Shell Variables
Variables inside single quotes ' don't get expanded by POSIX compatible shells, so using a shell
variable in a sed substitution requires the use of double quotes " instead of single quotes ':
$ var="he"
$ echo "hello" | sed "s/$var/XX/"
XXllo
$ var="he"
$ echo "hello" | sed 's/$var/XX/'
hello
$ var='./&/;x;w/etc/passwd
> x;s/he'
$ echo "hello" | sed "s/$var/XX/"
sed: /etc/passwd: Permission denied
If the above was run as root the output would have been indistinguishable from the first example,
and the contents of /etc/passwd would be destroyed.
Backreference
Using escaped brackets, you can define a capturing group in a pattern that can be backreferenced
in the substitution string with \1:
When using extended regular expressions (see Additional Options) parenthesis perform grouping
by default, and do not have to be escaped:
https://riptutorial.com/ 25
Words consisting of letter, digits and underscores can be matched using the expression
[[:alnum:]_]\{1,\}:
GNU sed
[2addr] s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the pattern space. Any
character other than backslash or newline can be used instead of a slash to delimit
the BRE and the replacement. Within the BRE and the replacement, the BRE delimiter
itself can be used as a literal character if it is preceded by a backslash.
There are cases when the delimiter / for sed replacement is in the BRE or replacement, triggering
errors like:
If we want to replace only the first occurrence in a line, we use sed as usual:
$ cat example
aaaaabbbbb
aaaaaccccc
aaaaaddddd
$ sed 's/a/x/' example
xaaaabbbbb
xaaaaccccc
https://riptutorial.com/ 26
xaaaaddddd
And if we want to replace one specific occurrence, we can actually specify which one:
GNU sed
From info sed, see GNU sed manual for online version
the POSIX standard does not specify what should happen when you mix the g and
NUMBER modifiers, and currently there is no widely agreed upon meaning across sed
implementations. For GNU sed, the interaction is defined to be: ignore matches before
the NUMBERth, and then match and replace all matches from the NUMBERth on.
https://riptutorial.com/ 27
Credits
S.
Chapters Contributors
No
Advanced sed
4 Raju
commands
https://riptutorial.com/ 28