Skip to main content

Understanding the `sed` - Stream Editor

The sed command in Linux stands for "stream editor" and is used to perform basic text transformations on an input stream (a file or input from a pipeline). It's an incredibly powerful tool for text processing.

Syntax of sed

The basic syntax of sed is:

sed [OPTIONS]... {script} [input-file]...

Here, the script is a set of commands that sed will execute, and input-file is the file that sed will read.

Understanding sed Command Syntax

The sed script syntax, like s/sed/SED/, follows a pattern that is common for many command-line text processing utilities. Understanding how to construct sed scripts is critical for users to apply their own transformations to text data effectively.

A sed script is a set of commands passed to sed to perform on the text. The most common command is s, for substitution. The syntax for a substitution command in sed is:

s/PATTERN/REPLACEMENT/FLAGS
  • s — Indicates a substitution operation.
  • PATTERN — The regular expression pattern to search for.
  • REPLACEMENT — The text to replace the matched pattern.
  • FLAGS — Optional flags that alter the behavior of the substitution. For example, g for global replacement.

Available Operartors For Script Patterns

Certainly! Below is a table summarizing various sed operations, their descriptions, and example scripts for each.

OperatorDescriptionExample ScriptExplanation
sSubstitutes the first occurrence of a pattern within a line.sed 's/foo/bar/'Replaces the first instance of foo with bar in each line.
gWhen used with s, it substitutes all occurrences within a line (global replacement).sed 's/foo/bar/g'Replaces all instances of foo with bar in each line.
pPrints the pattern space (normally, sed only outputs the changed lines). Used with -n to print changed lines.sed -n 's/foo/bar/p'Replaces the first instance of foo with bar and prints only the lines where substitution has occurred.
dDeletes lines or a range of lines.sed '/foo/d'Deletes all lines containing foo.
iInserts text before a line.sed '/foo/i\bar'Inserts the text bar before each line containing foo.
aAppends text after a line.sed '/foo/a\bar'Appends the text bar after each line containing foo.
cChanges lines matching a pattern to new text.sed '/foo/c\bar'Changes lines containing foo to bar.
yTransforms characters (like tr in shell).sed 'y/foo/bar/'Translates each character in foo to the corresponding character in bar.
nReads the next line of input into the pattern space.sed '/foo/{n;s/foo/bar/;}'If a line contains foo, it moves to the next line and then replaces foo with bar in that line.
qQuits sed after processing the specified number of lines.sed '10q'Quits sed after processing the first 10 lines.
=Prints the current line number.sed '/foo/='Prints the line numbers of lines containing foo.
rReads a file into the buffer and appends it to the pattern space when a match is found.sed '/foo/r bar.txt'Appends the contents of bar.txt after lines containing foo.
wWrites the pattern space to a file.sed -n '/foo/w bar.txt'Writes only the lines containing foo to bar.txt.
{}Groups several commands to be executed sequentially.sed '/foo/{s/foo/bar/;s/baz/qux/;}'On lines containing foo, replaces foo with bar, and then replaces baz with qux.
!Applies the command to all lines that do not match the pattern.sed '/foo/!s/bar/baz/'On lines that do not contain foo, replaces bar with baz.

These are some of the most commonly used sed operations. The sed command is extremely powerful and has many more options and intricacies that you can explore in the sed man pages or detailed sed tutorials. The sed manual can be accessed on the command line using man sed.

How to Use sed Scripts:

  • To use these sed scripts, you typically echo a string or pipe a file into sed. For example:

    echo "This is foo with baz" | sed 's/foo/bar/g'
  • This will output the string "This is bar with baz" as the script replaces " foo" with "bar" globally on each line.

  • To apply sed to a file, you can redirect a file into it or specify the filename at the end of the sed command. For example:

    sed 's/foo/bar/g' filename.txt
  • To save the changes back into the file, you can use the -i option to edit the file in-place. Note that -i

might not be supported in some versions of sed, or it might require an empty string argument (-i '') on BSD systems, including macOS.

sed -i 's/foo/bar/g' filename.txt

Commonly Used Options in sed

Here is a table of commonly used options with sed:

OptionShorthandDescription
--versionDisplay the version of sed.
--helpDisplay a help message.
-nSuppress automatic printing; sed will only print when explicitly told to.
-e scriptAdd the script to the commands to be executed.
-f fileAdd the contents of file to the commands to be executed.
-i[SUFFIX]Edit files in-place (makes backup if extension supplied).

Creating an Example File with vim

Before diving into examples, let's create a sample file named example.txt using vim:

vim example.txt

Press i to switch to insert mode and enter the following text:

This is a sed example.
sed is a stream editor.
Linux is fun.

Press Esc, then type :wq and press Enter to save and exit vim.

Examples of Using sed

Example 1: Simple Text Replacement

sed 's/sed/SED/' example.txt

Explanation:

  • This command tells sed to search for the string sed and replace it with SED in the file example.txt.
  • The s in 's/sed/SED/' is the substitution command.
  • The text pattern to search for (sed) and the replacement text (SED) are delimited by /.

Example 2: In-place Editing

sed -i 's/Linux/UNIX/' example.txt

Explanation:

  • The -i option makes sed edit the file in-place. sed will directly modify the file example.txt if this option is used.
  • This command replaces the first occurrence of Linux with UNIX in each line.

Example 3: Global Replacement

sed 's/editor/EDITOR/g' example.txt

Explanation:

  • The g at the end of the script tells sed to perform the substitution globally on each line, not just the first occurrence.

Example 4: Print Only Matching Lines

sed -n '/sed/p' example.txt

Explanation:

  • The -n option combined with the p command tells sed to only print the lines that match the pattern /sed/.

Example 5: Delete Lines

sed '/fun/d' example.txt

Explanation:

  • This command uses the d command to delete every line that matches the pattern /fun/.

Example 6: Combining Multiple sed Commands

sed -e 's/sed/SED/' -e '/Linux/d' example.txt

Explanation:

  • The -e option allows you to specify multiple editing commands. This command will first replace sed with SED, then delete lines containing Linux.

Example 7: Using a sed Script File

First, create a sed script file named script.sed:

vim script.sed

Press i and input:

s/editor/EDITOR/g
/Linux/d

Save and exit as before.

Now, run sed with the script file:

sed -f script.sed example.txt

Explanation:

  • The -f option tells sed to read the sed commands from the file script.sed.
  • sed will perform a global replacement of editor to EDITOR and delete lines containing Linux.

Using sed, one can perform sophisticated text processing tasks. The command's syntax and options allow for extensive manipulation of text, from simple replacements to complex transformations. It's an essential tool for anyone who works with text files or streams in Linux.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle