Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I initially learned to do sed search and replace like so:

sed -e 's/<stuff>/<replacement>/'

However I recently learned that these will do something different based on what command character you use at the beginning (instead of s). For example:

sed -e 'g/<stuff>/<??>/'

What are the different commands you can use at the beginning of a sed script, and what do they do?

share|improve this question
    
Check out Sed & Awk, Chapters 5 and 6. –  Ярослав Рахматуллин Jul 26 '13 at 8:56
add comment

1 Answer

I have made this answer a community-wiki post because it contains work that is not my own. Feel free to contribute to this answer.

The letters you see are called commands. There are dozens of them. To fully understand them, you must first understand a bit about sed's syntax and terminology. For an explanation of sed's terminology, including the terms pattern space and hold space, see this answer from StackOverflow.

sed syntax looks like this:

[address or range]command

You may run multiple commands in series by separating them with newlines or semicolons (though, some commands must be separated by newlines).

There are three types of commands.

  1. Those that do not accept addresses or ranges.
  2. Those that accept either one address or no addresses.
  3. Those that accept address ranges.

Below you will find a nearly-complete list of sed commands from the sed(1) manpage. I'm working on providing better explanations for these commands.

Zero- or one-address commands

These are 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 extension.
  • 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.

Address range commands

  • b label - Branch to label; if label is omitted, branch to end of script.
  • c \ text - Replace theHere are a few (taken from the sed(1) manpage, formatted, and slightly modified): 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 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 portion 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/// command 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/// command 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 extension.
  • 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 corresponding character in dest.

For more information about these commands, see the sed Texinfo manual.

A note on ranges

Some sed commands accept addresses or address ranges. For example,

sed 1p

would print line number one of sed's input.

sed /foo/p

would print all lines containing the word foo.

sed /foo/,/bar/p

would print all lines from foo to bar. Note that this may match multiple times. For example,

$ cat file
foo
baz
bar
baz
foo
bar
$ sed -n /foo/,/bar/p

would print

foo
baz
bar
foo
bar
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.