Post

Streamlined Text Editing with Sed: An In-depth Analysis of Stream Editor Functionality in Unix-based Systems

The Stream Editor, commonly abbreviated as sed, stands as a quintessential tool within the Unix and Linux ecosystems, heralded for its prowess in streamlining and automating text editing operations across a multitude of data processing scenarios. Conceived as a text processing utility by Lee E. McMahon in the early 1970s, sed has since evolved into a versatile and powerful command-line tool, renowned for its efficiency, flexibility, and scalability in handling textual transformations.

At its core, sed operates on the principle of parsing and modifying text streams through a series of user-defined commands, enabling users to perform a diverse range of editing tasks, such as search and replace, text substitution, line insertion/deletion, and pattern matching, with precision and ease. Its lightweight design and seamless integration with shell scripting languages make sed an indispensable asset for automating complex text manipulation workflows and batch processing tasks in both academic and professional computing environments.

Some common options for the sed command in Linux along with descriptions for each option:

OptionDescription
-e <script>Add the script to the commands to be executed.
-f <script_file>Read the script from the file.
-iEdit files in-place.
-lEnable line-by-line processing, which is faster but suitable only for single-line edits.
-nSuppress automatic printing of input lines.
-rUse extended regular expressions in the script.
-uForce output to be unbuffered.
-VOutput version information and exit.
-zTreat the input as a set of lines separated by null bytes, not lines ending in a newline character.

Common Options:

sed -e <script>

The sed command in Linux is a stream editor that is used to perform basic text transformations on an input stream (a file or input from a pipeline). The -e option is used to specify a script of editing commands to be applied to the input.

Here are some advanced examples demonstrating the usage of sed with the -e <script> option:

Example 1: replace text in file

Replace the text “old_text” with “new_text” in a file named example.txt:

1
sed -e 's/old_text/new_text/g' example.txt

In this example:

  • -e 's/old_text/new_text/g': Specifies a sed script to substitute (s) “old_text” with “new_text” globally (g) in the input.

Example 2: Multiple Editing Commands

Perform multiple editing commands on a file named example.txt to replace “old_text” with “new_text” and delete lines containing “delete_text”:

1
sed -e 's/old_text/new_text/g' -e '/delete_text/d' example.txt

In this example:

  • -e 's/old_text/new_text/g': Specifies a sed script to substitute “old_text” with “new_text” globally in the input.
  • -e '/delete_text/d': Specifies another sed script to delete (d) lines containing “delete_text” from the input.

Example 3: Use of Extended Regular Expressions

Use extended regular expressions (ERE) with sed to match lines starting with “start” and replace “start” with “end_”:

1
sed -E -e 's/^start_/end_/' example.txt

In this example:

  • -E: Enables extended regular expressions (ERE) for pattern matching in sed.
  • -e 's/^start_/end_/': Specifies a sed script to substitute “start” at the beginning of lines (^) with “end” in the input.

Example 4: Use of Capture Groups

Use capture groups with sed to swap “word1” and “word2” in a file named example.txt:

1
sed -e 's/\(word1\) \(word2\)/\2 \1/g' example.txt

In this example:

  • -e 's/\(word1\) \(word2\)/\2 \1/g': Specifies a sed script to capture “word1” and “word2” using capture groups (\( ... \)) and swap them in the input.

Example 5: In-Place Editing

Perform in-place editing of a file named example.txt to replace “old_text” with “new_text” and create a backup of the original file with a .bak extension:

1
sed -i.bak -e 's/old_text/new_text/g' example.txt

In this example:

  • -i.bak: Enables in-place editing (-i) and creates a backup of the original file with a .bak extension.
  • -e 's/old_text/new_text/g': Specifies a sed script to substitute “old_text” with “new_text” globally in the input.

These examples demonstrate how to use the sed command with the -e <script> option to specify a script of editing commands for performing various text transformations on input streams. The -e option provides flexibility to apply multiple editing commands, use extended regular expressions, capture groups, and perform in-place editing based on your specific text processing requirements and environment.

sed -f <script_file>

The sed command in Linux is a powerful stream editor that allows for basic text transformations on an input stream (a file or input from a pipeline). The -f <script_file> option is used to specify a script file containing sed commands to be applied to the input.

Here are some advanced examples demonstrating the usage of sed with the -f <script_file> option:

Example 1: **Create a Script File with Single Editing Command**

Create a script file named script.sed containing a single editing command to replace “old_text” with “new_text”:

script.sed

1
s/old_text/new_text/g

Apply the editing command from script.sed to a file named example.txt:

1
sed -f script.sed example.txt

In this example:

  • -f script.sed: Specifies a script file (script.sed) containing a sed command to be applied to the input.
  • example.txt: Specifies the input file to apply the sed command from script.sed.

Example 2: Create a Script File with Multiple Editing Commands

Create a script file named script.sed containing multiple editing commands to replace “old_text” with “new_text” and delete lines containing “delete_text”:

script.sed

1
2
s/old_text/new_text/g
/delete_text/d

Apply the editing commands from script.sed to a file named example.txt:

1
sed -f script.sed example.txt

In this example:

  • -f script.sed: Specifies a script file (script.sed) containing multiple sed commands to be applied to the input.
  • example.txt: Specifies the input file to apply the sed commands from script.sed.

Example 3: Use of Extended Regular Expressions

Create a script file named script.sed containing an extended regular expression (ERE) to match lines starting with “start” and replace “start” with “end_”:

script.sed

1
s/^start_/end_/

Apply the editing command from script.sed to a file named example.txt:

1
sed -E -f script.sed example.txt

In this example:

  • -E: Enables extended regular expressions (ERE) for pattern matching in sed.
  • -f script.sed: Specifies a script file (script.sed) containing a sed command to be applied to the input.
  • example.txt: Specifies the input file to apply the sed command from script.sed.

Example 4: Use of Capture Groups

Create a script file named script.sed containing a sed command with capture groups to swap “word1” and “word2”:

script.sed

1
s/\(word1\) \(word2\)/\2 \1/g

Apply the editing command from script.sed to a file named example.txt:

1
sed -f script.sed example.txt

In this example:

  • -f script.sed: Specifies a script file (script.sed) containing a sed command to be applied to the input.
  • example.txt: Specifies the input file to apply the sed command from script.sed.

Example 5: In-Place Editing with Script File

Create a script file named script.sed containing a sed command to replace “old_text” with “new_text” and perform in-place editing:

script.sed

1
s/old_text/new_text/g

Apply the editing command from script.sed to a file named example.txt and create a backup of the original file with a .bak extension:

1
sed -i.bak -f script.sed example.txt

In this example:

  • -i.bak: Enables in-place editing (-i) and creates a backup of the original file with a .bak extension.
  • -f script.sed: Specifies a script file (script.sed) containing a sed command to be applied to the input.
  • example.txt: Specifies the input file to apply the sed command from script.sed.

These examples demonstrate how to use the sed command with the -f <script_file> option to specify a script file containing sed commands for performing various text transformations on input streams. The -f option provides flexibility to apply multiple editing commands, use extended regular expressions, capture groups, and perform in-place editing based on your specific text processing requirements and environment.

sed -i

Certainly! The -i option in sed allows for in-place editing of files, meaning it directly modifies the input file rather than outputting the changes to the standard output. Here are some advanced examples demonstrating the usage of sed with the -i option:

Example 1: Replace Text in Multiple Files

Replace “old_text” with “new_text” in multiple files (file1.txt, file2.txt, file3.txt) using the -i option:

1
sed -i 's/old_text/new_text/g' file1.txt file2.txt file3.txt

In this example:

  • -i: Enables in-place editing, modifying the files directly.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally in each file.

Example 2: Create a Backup of Original File with .bak Extension

Replace “old_text” with “new_text” in a file named example.txt and create a backup of the original file with a .bak extension:

1
sed -i.bak 's/old_text/new_text/g' example.txt

In this example:

  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally in example.txt.

Example 3: Delete Lines Matching a Pattern

Delete lines containing “delete_text” from a file named example.txt and create a backup of the original file with a .bak extension:

1
sed -i.bak '/delete_text/d' example.txt

In this example:

  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • /delete_text/d: Specifies the sed command to delete lines containing “delete_text” from example.txt.

Example 4: Perform Multiple Editing Operations

Perform multiple editing operations (replace “old_text” with “new_text” and delete lines containing “delete_text”) in a file named example.txt and create a backup of the original file with a .bak extension:

1
sed -i.bak -e 's/old_text/new_text/g' -e '/delete_text/d' example.txt

In this example:

  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • -e 's/old_text/new_text/g': Specifies the first sed command to replace “old_text” with “new_text” globally.
  • -e '/delete_text/d': Specifies the second sed command to delete lines containing “delete_text”.

Example 5: Conditional Replacement

Perform conditional text replacement based on a pattern in a file named example.txt and create a backup of the original file with a .bak extension:

1
sed -i.bak '/pattern/s/old_text/new_text/g' example.txt

In this example:

  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • /pattern/s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally only on lines containing “pattern”.

These advanced examples demonstrate various ways to use the sed command with the -i option for in-place editing, allowing you to directly modify files while retaining the flexibility and power of sed for text transformation and manipulation in Linux.

sed -l

The -l option in sed is used to enable line buffering. When line buffering is enabled, sed processes input one line at a time, which can be useful in certain scenarios to ensure that each line of input is processed individually rather than waiting for a complete buffer. Here are some advanced examples demonstrating the usage of sed with the -l option:

Example 1: Line Buffering with Text Replacement

Replace “old_text” with “new_text” in each line of a file named example.txt using line buffering:

1
sed -l 's/old_text/new_text/g' example.txt

In this example:

  • -l: Enables line buffering, processing input one line at a time.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally in each line of example.txt.

Example 2: Line Buffering with Conditional Replacement

Perform conditional text replacement based on a pattern in each line of a file named example.txt using line buffering:

1
sed -l '/pattern/s/old_text/new_text/g' example.txt

In this example:

  • -l: Enables line buffering, processing input one line at a time.
  • /pattern/s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally only on lines containing “pattern” in example.txt.

Example 3: Line Buffering with Multiple Editing Commands

Combine multiple sed commands for text replacement and deletion of lines in each line of a file named example.txt using line buffering:

1
sed -l -e 's/old_text/new_text/g' -e '/delete_text/d' example.txt

In this example:

  • -l: Enables line buffering, processing input one line at a time.
  • -e 's/old_text/new_text/g': Specifies the first sed command to replace “old_text” with “new_text” globally in each line.
  • -e '/delete_text/d': Specifies the second sed command to delete lines containing “delete_text”.

Example 4: Line Buffering with Capture Groups

Use capture groups to rearrange parts of matched patterns in each line of a file named example.txt using line buffering:

1
sed -l 's/\(word1\) \(word2\)/\2 \1/g' example.txt

In this example:

  • -l: Enables line buffering, processing input one line at a time.
  • s/\(word1\) \(word2\)/\2 \1/g: Specifies the sed command with capture groups to swap “word1” and “word2” in each line of example.txt.

Example 5: Line Buffering with In-Place Editing

Perform text replacement in each line of a file named example.txt using line buffering and create a backup of the original file with a .bak extension:

1
sed -l -i.bak 's/old_text/new_text/g' example.txt

In this example:

  • -l: Enables line buffering, processing input one line at a time.
  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally in each line.

These advanced examples demonstrate various ways to use the sed command with the -l option for line buffering, allowing you to process each line of input individually for text transformation and manipulation in Linux.

sed -n

The -n option in sed suppresses automatic printing of pattern space, allowing you to control when and what is printed using the p command or other sed commands. This option is useful for selective printing, filtering, and processing of text based on specified conditions. Here are some advanced examples demonstrating the usage of sed with the -n option:

Example 1: Suppress Automatic Printing

Suppress automatic printing of pattern space in a file named example.txt:

1
sed -n 'p' example.txt

In this example:

  • -n: Suppresses automatic printing of pattern space.
  • p: Specifies the sed command to print each line of example.txt.

Example 2: Selective Printing with Conditional Replacement

Print lines containing “pattern” and replace “old_text” with “new_text” in each line of a file named example.txt:

1
sed -n '/pattern/{s/old_text/new_text/g;p}' example.txt

In this example:

  • -n: Suppresses automatic printing of pattern space.
  • /pattern/: Specifies the condition to match lines containing “pattern”.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text”.
  • p: Specifies the sed command to print lines that meet the specified condition.

Example 3: Selective Printing with Multiple Editing Commands

Combine multiple sed commands for text replacement and deletion of lines, and selectively print lines in a file named example.txt:

1
sed -n -e '/pattern/{s/old_text/new_text/g;p}' -e '/delete_text/d' example.txt

In this example:

  • -n: Suppresses automatic printing of pattern space.
  • /pattern/: Specifies the condition to match lines containing “pattern”.
  • s/old_text/new_text/g: Specifies the first sed command to replace “old_text” with “new_text”.
  • p: Specifies the sed command to print lines that meet the specified condition.
  • /delete_text/d: Specifies the second sed command to delete lines containing “delete_text”.

Example 4: Selective Printing with Capture Groups

Use capture groups to rearrange parts of matched patterns and selectively print lines in a file named example.txt:

1
sed -n 's/\(word1\) \(word2\)/\2 \1/p' example.txt

In this example:

  • -n: Suppresses automatic printing of pattern space.
  • s/\(word1\) \(word2\)/\2 \1/p: Specifies the sed command with capture groups to swap “word1” and “word2” and print lines where the replacement occurs.

Example 5: Selective Printing with Line Numbers

Print lines with line numbers and replace “old_text” with “new_text” only in specific line numbers in a file named example.txt:

1
sed -n '5,10{s/old_text/new_text/g;p}' example.txt

In this example:

  • -n: Suppresses automatic printing of pattern space.
  • 5,10: Specifies the range of line numbers (from line 5 to line 10).
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text”.
  • p: Specifies the sed command to print lines within the specified line number range where the replacement occurs.

These advanced examples demonstrate various ways to use the sed command with the -n option for selective printing and processing of text based on specified conditions, providing greater control over text transformation and manipulation in Linux.

sed -r

The -r option in sed enables extended regular expressions (ERE) for pattern matching, allowing you to use more complex and powerful regular expressions without needing to escape certain characters. Here are some advanced examples demonstrating the usage of sed with the -r option:

Example 1: **Extended Regular Expressions for Matching**

Use extended regular expressions to match lines starting with “start” and replace “start” with “end_” in a file named example.txt:

1
sed -r 's/^start_/end_/' example.txt

In this example:

  • -r: Enables extended regular expressions (ERE) for pattern matching.
  • s/^start_/end_/: Specifies the sed command to match lines starting with “start” and replace “start” with “end_”.

Example 2: Extended Regular Expressions with Capture Groups

Use extended regular expressions with capture groups to rearrange parts of matched patterns in a file named example.txt:

1
sed -r 's/(word1) (word2)/\2 \1/' example.txt

In this example:

  • -r: Enables extended regular expressions (ERE) for pattern matching.
  • s/(word1) (word2)/\2 \1/: Specifies the sed command with capture groups to swap “word1” and “word2”.

Example 3: Extended Regular Expressions with Conditional Replacement

Use extended regular expressions to match lines containing “pattern” and replace “old_text” with “new_text” in a file named example.txt:

1
sed -r '/pattern/s/old_text/new_text/g' example.txt

In this example:

  • -r: Enables extended regular expressions (ERE) for pattern matching.
  • /pattern/: Specifies the condition to match lines containing “pattern”.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” on lines containing “pattern”.

Example 4: Extended Regular Expressions with Multiple Editing Commands

Combine multiple sed commands using extended regular expressions for text replacement and deletion of lines in a file named example.txt:

1
sed -r -e 's/old_text/new_text/g' -e '/delete_text/d' example.txt

In this example:

  • -r: Enables extended regular expressions (ERE) for pattern matching.
  • -e 's/old_text/new_text/g': Specifies the first sed command to replace “old_text” with “new_text”.
  • -e '/delete_text/d': Specifies the second sed command to delete lines containing “delete_text”.

Example 5: Extended Regular Expressions with In-Place Editing

Perform text replacement using extended regular expressions in a file named example.txt and create a backup of the original file with a .bak extension:

1
sed -r -i.bak 's/old_text/new_text/g' example.txt

In this example:

  • -r: Enables extended regular expressions (ERE) for pattern matching.
  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text”.

These advanced examples demonstrate various ways to use the sed command with the -r option for extended regular expressions, allowing you to leverage more complex pattern matching capabilities for text transformation and manipulation in Linux.

sed -u

The -u option in sed enables unbuffered output, which means that each line of output is flushed immediately rather than being buffered. This can be particularly useful in scenarios where real-time processing or immediate output is required. Here are some advanced examples demonstrating the usage of sed with the -u option:

Example 1: **Unbuffered Output with Text Replacement**

Replace “old_text” with “new_text” in each line of a file named example.txt using unbuffered output:

1
sed -u 's/old_text/new_text/g' example.txt

In this example:

  • -u: Enables unbuffered output, flushing each line of output immediately.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally in each line of example.txt.

Example 2: Unbuffered Output with Conditional Replacement

Perform conditional text replacement based on a pattern in each line of a file named example.txt using unbuffered output:

1
sed -u '/pattern/s/old_text/new_text/g' example.txt

In this example:

  • -u: Enables unbuffered output, flushing each line of output immediately.
  • /pattern/: Specifies the condition to match lines containing “pattern”.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” on lines containing “pattern”.

Example 3: Unbuffered Output with Multiple Editing Commands

Combine multiple sed commands for text replacement and deletion of lines, and use unbuffered output in a file named example.txt:

1
sed -u -e 's/old_text/new_text/g' -e '/delete_text/d' example.txt

In this example:

  • -u: Enables unbuffered output, flushing each line of output immediately.
  • -e 's/old_text/new_text/g': Specifies the first sed command to replace “old_text” with “new_text”.
  • -e '/delete_text/d': Specifies the second sed command to delete lines containing “delete_text”.

Example 4: Unbuffered Output with Capture Groups

Use capture groups to rearrange parts of matched patterns and use unbuffered output in a file named example.txt:

1
sed -u 's/\(word1\) \(word2\)/\2 \1/' example.txt

In this example:

  • -u: Enables unbuffered output, flushing each line of output immediately.
  • s/\(word1\) \(word2\)/\2 \1/: Specifies the sed command with capture groups to swap “word1” and “word2”.

Example 5: Unbuffered Output with In-Place Editing

Perform text replacement in each line of a file named example.txt using unbuffered output and create a backup of the original file with a .bak extension:

1
sed -u -i.bak 's/old_text/new_text/g' example.txt

In this example:

  • -u: Enables unbuffered output, flushing each line of output immediately.
  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text”.

These advanced examples demonstrate various ways to use the sed command with the -u option for unbuffered output, allowing you to process text in real-time or with immediate output in Linux.

sed -v

The -v option in sed enables verbose mode, which can provide additional information or diagnostics during the execution of sed commands. However, it’s important to note that the -v option is not widely used with sed in the same way as with some other commands like grep.

Here’s an example illustrating the -v option with sed:

Example: Verbose Mode with sed

1
sed -v 's/old_text/new_text/g' example.txt

In this example:

  • -v: Enables verbose mode in sed. Depending on the sed implementation and version, this may display additional information or diagnostics during the execution of the sed command.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally in example.txt.

It’s important to check the specific sed implementation and version you are using to understand the exact behavior and output of the -v option, as its usage and effects can vary.

Generally speaking, the -v option is not commonly used with sed for text transformation and manipulation. Most sed commands are executed silently without verbose output unless there are errors or warnings that need to be displayed. If you’re looking for more detailed information or diagnostics when using sed, you may need to rely on other debugging or logging techniques rather than the -v option.

sed -z

The -z option in sed allows for null-byte delimited input instead of newline-delimited input. This is particularly useful for processing null-terminated strings, such as those produced by some find and xargs commands. Here are some advanced examples demonstrating the usage of sed with the -z option:

Example 1: Null-Byte Delimited Text Replacement

Replace “old_text” with “new_text” in null-byte delimited input from a file named example.txt:

1
sed -z 's/old_text/new_text/g' example.txt

In this example:

  • -z: Enables null-byte delimited input.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” globally in the null-byte delimited input from example.txt.

Example 2: Null-Byte Delimited Conditional Replacement

Perform conditional text replacement based on a pattern in null-byte delimited input from a file named example.txt:

1
sed -z '/pattern/s/old_text/new_text/g' example.txt

In this example:

  • -z: Enables null-byte delimited input.
  • /pattern/: Specifies the condition to match lines containing “pattern”.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” on lines containing “pattern” in the null-byte delimited input from example.txt.

Example 3: Null-Byte Delimited Multiple Editing Commands

Combine multiple sed commands for text replacement and deletion of lines, and process null-byte delimited input from a file named example.txt:

1
sed -z -e 's/old_text/new_text/g' -e '/delete_text/d' example.txt

In this example:

  • -z: Enables null-byte delimited input.
  • -e 's/old_text/new_text/g': Specifies the first sed command to replace “old_text” with “new_text”.
  • -e '/delete_text/d': Specifies the second sed command to delete lines containing “delete_text”.

Example 4: Null-Byte Delimited Capture Groups

Use capture groups to rearrange parts of matched patterns in null-byte delimited input from a file named example.txt:

1
sed -z 's/\(word1\) \(word2\)/\2 \1/' example.txt

In this example:

  • -z: Enables null-byte delimited input.
  • s/\(word1\) \(word2\)/\2 \1/: Specifies the sed command with capture groups to swap “word1” and “word2” in the null-byte delimited input from example.txt.

Example 5: Null-Byte Delimited In-Place Editing

Perform text replacement in null-byte delimited input from a file named example.txt using in-place editing and create a backup of the original file with a .bak extension:

1
sed -z -i.bak 's/old_text/new_text/g' example.txt

In this example:

  • -z: Enables null-byte delimited input.
  • -i.bak: Enables in-place editing and creates a backup of the original file (example.txt) with a .bak extension.
  • s/old_text/new_text/g: Specifies the sed command to replace “old_text” with “new_text” in the null-byte delimited input.

These advanced examples demonstrate various ways to use the sed command with the -z option for processing null-byte delimited input, allowing you to work with null-terminated strings and perform text transformation and manipulation in Linux.

This post is licensed under CC BY 4.0 by the author.