sed – A Powerful Streaming Text Editor

Sed is a powerful, command-line text editor used to manipulate text files. It is designed to work with streams of text, making it an ideal tool for processing large files or automating tasks. Sed can perform a wide range of text transformations, including search and replace, deletion, insertion, and more.


Sed operates by reading input from a file or standard input stream (stdin), applying a series of commands to the text, and then printing the result to standard output (stdout). The basic syntax for sed is as follows:

sed [options] 'command' file

Here, [options] are any command-line options you want to pass to sed, 'command' is the series of sed commands you want to apply, and file is the name of the file you want to process.

Sed commands consist of an action (such as s for substitute or d for delete), followed by a pattern to match, and then an optional replacement string. For example, the following sed command replaces all occurrences of “foo” with “bar” in the file input.txt:

sed 's/foo/bar/g' input.txt

In this command, s is the substitute command, foo is the pattern to match, bar is the replacement string, and g is a flag that tells sed to apply the command globally (i.e., to replace all occurrences of “foo”, not just the first one).

Sed also supports a variety of other commands, including d (delete), i (insert), a (append), y (transliterate), and more. For a complete list of sed commands and their syntax, see the sed man page (man sed).


Here are some common use cases for sed:

  • Search and replace: Replace all occurrences of “foo” with “bar” in a file:
  sed 's/foo/bar/g' input.txt
  • Delete lines: Delete all lines containing the word “foo” in a file:
  sed '/foo/d' input.txt
  • Insert text: Insert the line “Hello, world!” before every line in a file:
  sed 'iHello, world!' input.txt
  • Append text: Append the line “Goodbye, world!” after every line in a file:
  sed 'aGoodbye, world!' input.txt
  • Transliterate text: Replace all occurrences of “a” with “A”, “b” with “B”, etc. in a file:
  sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' input.txt


Here are some of the most commonly used options for sed:

Option Description
-n Suppress automatic printing of pattern space.
-e <command> Add the specified command to the list of commands to be executed.
-f <file> Read commands from the specified file instead of the command-line.
-i Edit files in place (i.e., modify the file directly instead of writing to standard output).

For a complete list of sed options, see the sed man page (man sed).

Troubleshooting Tips

Here are some common issues you may encounter when using sed, and how to troubleshoot them:

  • Sed isn’t doing anything: Make sure you’re passing sed a valid file to process, and that your sed command is properly formatted. You can also try using the -n option to disable automatic printing of pattern space, so you can see exactly what sed is doing.
  • Sed is modifying my file incorrectly: Make sure you’re using the -i option if you want to modify the file in place. Also, be careful when using regular expressions in your sed commands, as they can sometimes match more or less than you intended.
  • Sed is giving me strange errors: Check your sed syntax carefully, and make sure you’re using the correct command syntax for the task you want to perform. You can also try running sed with the -e option to execute a single command at a time, so you can isolate the problem.


  • Sed is a powerful tool, but it can be complex to use. Be sure to read the sed man page (man sed) carefully, and experiment with sed on small files before trying to process larger ones.
  • Sed can be used in conjunction with other Linux tools, such as grep, awk, and cut, to perform complex text processing tasks.