# man > SED(1)

> **TLDR:** Edit text in a scriptable manner.
>
- Replace all `apple` (basic `regex`) occurrences with `mango` (basic `regex`) in all input lines and print the result to `stdout`:
  `{{command}} | sed 's/apple/mango/g'`
- Execute a specific script [f]ile and print the result to `stdout`:
  `{{command}} | sed -f {{path/to/script.sed}}`
- Print just a first line to `stdout`:
  `{{command}} | sed -n '1p'`

*Source: tldr-pages*

---

[SED(1)](https://www.chedong.com/phpMan.php/man/SED/1/markdown)                                      User Commands                                     [SED(1)](https://www.chedong.com/phpMan.php/man/SED/1/markdown)



## NAME
       sed - stream editor for filtering and transforming text

## SYNOPSIS
       **sed** [_OPTION_]... _{script-only-if-no-other-script}_ [_input-file_]...

## DESCRIPTION
       _Sed_  is a stream editor.  A stream editor is used to perform basic text transformations on an
       input stream (a file or input from a pipeline).  While in some  ways  similar  to  an  editor
       which  permits  scripted  edits  (such as _ed_), _sed_ works by making only one pass over the in‐
       put(s), and is consequently more efficient.  But it is _sed_'s ability  to  filter  text  in  a
       pipeline which particularly distinguishes it from other types of editors.

### -n --quiet --silent

              suppress automatic printing of pattern space

### --debug

              annotate program execution

### -e --expression

              add the script to the commands to be executed

### -f --file

              add the contents of script-file to the commands to be executed

### --follow-symlinks

              follow symlinks when processing in place

### -i[SUFFIX] --in-place

              edit files in place (makes backup if SUFFIX supplied)

### -l --line-length

              specify the desired line-wrap length for the `l' command

### --posix

              disable all GNU extensions.

### -E -r --regexp-extended

              use extended regular expressions in the script (for portability use POSIX **-E**).

### -s --separate

              consider files as separate rather than as a single, continuous long stream.

### --sandbox

              operate in sandbox mode (disable e/r/w commands).

### -u --unbuffered

              load  minimal  amounts  of data from the input files and flush the output buffers more
              often

### -z --null-data

              separate lines by NUL characters

### --help
              display this help and exit

### --version
              output version information and exit

       If no **-e**, **--expression**, **-f**, or **--file** option is given, then the first non-option argument  is
       taken  as  the sed script to interpret.  All remaining arguments are names of input files; if
       no input files are specified, then the standard input is read.

       GNU sed home page: <<https://www.gnu.org/software/sed/>>.  General  help  using  GNU  software:
       <<https://www.gnu.org/gethelp/>>.  E-mail bug reports to: <<bug-sed@gnu.org>>.

## COMMAND SYNOPSIS
       This  is  just  a  brief synopsis of _sed_ commands to serve as a reminder to those who already
       know _sed_; other documentation (such as the texinfo document) must be consulted for fuller de‐
       scriptions.

### Zero-address ``commands''
       : _label_
              Label for **b** and **t** commands.

       #_comment_
              The comment extends until the next newline (or the end of a **-e** script fragment).

       }      The closing bracket of a { } block.

### Zero- or One- address commands
       =      Print the current line number.

       a \

       _text_   Append _text_, which has each embedded newline preceded by a backslash.

       i \

       _text_   Insert _text_, which has each embedded newline preceded by a backslash.

       q [_exit-code_]
              Immediately  quit  the  _sed_  script  without processing any more input, except that if
              auto-print is not disabled the current pattern space will be printed.  The  exit  code
              argument is a GNU extension.

       Q [_exit-code_]
              Immediately  quit the _sed_ script without processing any more input.  This is a GNU ex‐
              tension.

       r _filename_
              Append text read from _filename_.

       R _filename_
              Append a line read from _filename_.  Each invocation of the command reads  a  line  from
              the file.  This is a GNU extension.

### Commands which accept address ranges
       {      Begin a block of commands (end with a }).

       b _label_
              Branch to _label_; if _label_ is omitted, branch to end of script.

       c \

       _text_   Replace  the  selected  lines with _text_, which has each embedded newline preceded by a
              backslash.

       d      Delete pattern space.  Start next cycle.

       D      If pattern space contains no newline, start a normal new cycle as if the d command was
              issued.   Otherwise,  delete  text  in  the pattern space up to the first newline, and
              restart cycle with the resultant pattern space, without reading a new line of input.

       h H    Copy/append pattern space to hold space.

       g G    Copy/append hold space to pattern space.

       l      List out the current line in a ``visually unambiguous'' form.

       l _width_
              List out the current line in a ``visually unambiguous'' form,  breaking  it  at  _width_
              characters.  This is a GNU extension.

       n N    Read/append the next line of input into the pattern space.

       p      Print the current pattern space.

       P      Print up to the first embedded newline of the current pattern space.

       s/_regexp_/_replacement_/
              Attempt  to  match _regexp_ against the pattern space.  If successful, replace that por‐
              tion matched with _replacement_.  The _replacement_ may contain the special character **&** to
              refer  to  that portion of the pattern space which matched, and the special escapes \1
              through \9 to refer to the corresponding matching sub-expressions in the _regexp_.

       t _label_
              If a s/// has done a successful substitution since the last input line  was  read  and
              since  the  last  t or T command, then branch to _label_; if _label_ is omitted, branch to
              end of script.

       T _label_
              If no s/// has done a successful substitution since the last input line was  read  and
              since  the  last  t or T command, then branch to _label_; if _label_ is omitted, branch to
              end of script.  This is a GNU extension.

       w _filename_
              Write the current pattern space to _filename_.

       W _filename_
              Write the first line of the current pattern space to _filename_.  This is a  GNU  exten‐
              sion.

       x      Exchange the contents of the hold and pattern spaces.

       y/_source_/_dest_/
              Transliterate the characters in the pattern space which appear in _source_ to the corre‐
              sponding character in _dest_.

## Addresses
       _Sed_ commands can be given with no addresses, in which case the command will be  executed  for
       all  input lines; with one address, in which case the command will only be executed for input
       lines which match that address; or with two addresses, in which case the command will be exe‐
       cuted  for  all  input lines which match the inclusive range of lines starting from the first
       address and continuing to the second address.  Three things to note about address ranges: the
       syntax  is  _addr1_,_addr2_  (i.e., the addresses are separated by a comma); the line which _addr1_
       matched will always be accepted, even if _addr2_ selects an earlier line; and  if  _addr2_  is  a
       _regexp_, it will not be tested against the line that _addr1_ matched.

       After  the  address  (or  address-range), and before the command, a **!**  may be inserted, which
       specifies that the command shall only be executed if the address (or address-range) does  **not**
       match.

       The following address types are supported:

       _number_ Match  only the specified line _number_ (which increments cumulatively across files, un‐
              less the **-s** option is specified on the command line).

       _first_~_step_
              Match every _step_'th line starting with line _first_.  For example, ``sed -n 1~2p''  will
              print  all  the odd-numbered lines in the input stream, and the address 2~5 will match
              every fifth line, starting with the second.  _first_ can be zero; in this case, _sed_  op‐
              erates as if it were equal to _step_.  (This is an extension.)

       $      Match the last line.

       /_regexp_/
              Match lines matching the regular expression _regexp_.  Matching is performed on the cur‐
              rent pattern space, which can be modified with commands such as ``s///''.

       \**c**_regexp_**c**
              Match lines matching the regular expression _regexp_.  The **c** may be any character.

       GNU _sed_ also supports some special 2-address forms:

       0,_addr2_
              Start out in "matched first address" state, until _addr2_ is found.  This is similar  to
              1,_addr2_,  except  that  if _addr2_ matches the very first line of input the 0,_addr2_ form
              will be at the end of its range, whereas the 1,_addr2_ form will still be at the  begin‐
              ning of its range.  This works only when _addr2_ is a regular expression.

       _addr1_,+_N_
              Will match _addr1_ and the _N_ lines following _addr1_.

       _addr1_,~_N_
              Will  match  _addr1_  and the lines following _addr1_ until the next line whose input line
              number is a multiple of _N_.

## REGULAR EXPRESSIONS
       POSIX.2 BREs _should_ be supported, but they aren't completely because of performance problems.
       The  **\n** sequence in a regular expression matches the newline character, and similarly for **\a**,
       **\t**, and other sequences.  The _-E_ option switches to using extended  regular  expressions  in‐
       stead; it has been supported for years by GNU sed, and is now included in POSIX.

## BUGS
       E-mail  bug reports to **<bug-sed@gnu.org>**.  Also, please include the output of ``sed --version''
       in the body of your report if at all possible.

## AUTHOR
       Written by Jay Fenlason, Tom Lord, Ken Pizzini, Paolo Bonzini, Jim Meyering, and  Assaf  Gor‐
       don.

       This sed program was built with SELinux support.  SELinux is enabled on this system.

       GNU  sed  home  page:  <<https://www.gnu.org/software/sed/>>.  General help using GNU software:
       <<https://www.gnu.org/gethelp/>>.  E-mail bug reports to: <<bug-sed@gnu.org>>.

## COPYRIGHT
       Copyright © 2020 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3  or  later
       <<https://gnu.org/licenses/gpl.html>>.
       This  is free software: you are free to change and redistribute it.  There is NO WARRANTY, to
       the extent permitted by law.

## SEE ALSO
       [**awk**(1)](https://www.chedong.com/phpMan.php/man/awk/1/markdown), [**ed**(1)](https://www.chedong.com/phpMan.php/man/ed/1/markdown), [**grep**(1)](https://www.chedong.com/phpMan.php/man/grep/1/markdown), [**tr**(1)](https://www.chedong.com/phpMan.php/man/tr/1/markdown), [**perlre**(1)](https://www.chedong.com/phpMan.php/man/perlre/1/markdown), sed.info, any of various books on _sed_, the _sed_ FAQ
       (<http://sed.sf.net/grabbag/tutorials/sedfaq.txt>), <http://sed.sf.net/grabbag/>.

       The full documentation for **sed** is maintained as a Texinfo manual.  If the **info** and **sed** pro‐
       grams are properly installed at your site, the command

              **info** **sed**

       should give you access to the complete manual.



sed 4.8                                     January 2020                                      [SED(1)](https://www.chedong.com/phpMan.php/man/SED/1/markdown)
