How to Use the Command `sed` (with Examples)

How to Use the Command `sed` (with Examples)

The sed command, short for “stream editor,” is a powerful utility in Unix and Unix-like systems used to parse and transform text. It allows users to perform basic and advanced text manipulations directly from the command line. By using regular expressions, users can search, find, and edit text in a scriptable manner. sed is commonly used in shell scripts for batch-editing large volumes of text data efficiently.

Use Case 1: Replace All apple Occurrences with mango in All Input Lines and Print the Result to stdout

Code:

command | sed 's/apple/mango/g'

Motivation:

This elementary example of sed illustrates its capability to replace text efficiently in a stream of data. Replacing a specific word throughout a document or data stream is a typical task in text processing tasks, whether updating a product name or a variable placeholder.

Explanation:

  • command: Represents any command whose output serves as input to sed.
  • sed: Invokes the stream editor.
  • 's/apple/mango/g': The substitution command s/ replaces occurrences of apple with mango. g denotes global substitution, meaning every occurrence within a line is replaced.

Example Output:

Before sed:

apple pie, apple tart

After sed:

mango pie, mango tart

Use Case 2: Execute a Specific Script [f]ile and Print the Result to stdout

Code:

command | sed -f path/to/script.sed

Motivation:

When dealing with complex text replacement or transformation tasks, writing and maintaining a standalone script file can be easier than one-liners. This use case allows users to execute a sequence of sed commands stored in a file.

Explanation:

  • command: Any command that generates input for sed.
  • sed -f: Tells sed to execute scripts from the specified file.
  • path/to/script.sed: Path to the script file containing the sed commands.

Example Output:

If script.sed contains:

s/apple/mango/g
s/tart/pie/g

Input from a preceding command might look like:

apple tart
apple pie

The output would be:

mango pie
mango pie

Code:

command | sed -fa path/to/script.sed

Motivation:

This use case is beneficial for optimizing memory usage when managing large files. By delaying file opening, sed reduces resource consumption until absolutely necessary.

Explanation:

  • command: The assumed incoming data stream or file content.
  • sed -fa: Combines executing a script (as previously described) with delayed file opening.
  • path/to/script.sed: Path to the script file containing commands, potentially using the w function to write output.

Example Output:

While the exact output may vary depending on the script, the delay in file opening optimizes process efficiency, especially for large text files.

Use Case 4: Turn On GNU re[g]ex Extension

Code:

command | sed -fg path/to/script.sed

Motivation:

When dealing with complex text matching patterns, GNU regex extensions offer additional capabilities, such as non-greedy matching and nested group handling, which are not otherwise available in basic regex.

Explanation:

  • command: Standard input into the sed utility.
  • sed -fg: Engages both the script execution and the GNU regex extensions.
  • path/to/script.sed: The script file using potentially advanced regexes.

Example Output:

With a GNU-style regex, a script could replace non-greedy string matches effectively, which wouldn’t be possible in basic regex syntax, yielding more precise output transformations.

Use Case 5: Replace All apple Occurrences with APPLE Using Extended Regex in All Input Lines

Code:

command | sed -E 's/(apple)/\U\1/g'

Motivation:

Applying extended regex and additional commands (e.g., upper-casing transformations) makes sed an even more robust tool for text manipulations, particularly in format conversions required in certain data pipelines.

Explanation:

  • command: The source input.
  • sed -E: Signals the use of extended regular expressions.
  • 's/(apple)/\U\1/g': A substitution command capturing apple and replacing it with its uppercase equivalent APPLE.

Example Output:

Before sed:

apple tart
apple cider

After sed:

APPLE tart
APPLE cider

Use Case 6: Print Just the First Line to stdout

Code:

command | sed -n '1p'

Motivation:

Extracting specific lines, such as headers or titles, is commonly required for processing structured documents. This command facilitates quick extraction of the desired line.

Explanation:

  • command: Provides input lines.
  • sed -n: Suppresses automatic printing, allowing for selective line output.
  • '1p': Prints only the first line specified.

Example Output:

Input:

Header
Line 1
Line 2

Output:

Header

Use Case 7: Replace All apple Occurrences with mango in a Specific File and Overwrite the Original File in Place

Code:

sed -i 's/apple/mango/g' path/to/file

Motivation:

When modifications need to be applied directly to files rather than in-transit data, in-place editing simplifies the process without requiring intermediate files.

Explanation:

  • sed -i: Modifies files directly (-i stands for in-place).
  • 's/apple/mango/g': Substitution command replacing all occurrences within the specified file.
  • path/to/file: The target file for text transformation.

Example Output:

File before sed:

apple pie
apple tart

File after sed:

mango pie
mango tart

Conclusion:

The sed command remains a robust and indispensable tool for text manipulation in the command-line environment due to its versatile regular expression capabilities and wide range of functions for detailed text processing needs. By understanding and employing these examples, users can efficiently manipulate text data across various scenarios and applications.

Tags :

Related Posts

How to Use the Command 'csvstat' (with Examples)

How to Use the Command 'csvstat' (with Examples)

The csvstat command is a powerful tool included in the csvkit suite of utilities for handling CSV (Comma-Separated Values) files.

Read More
How to Use the Command 'navi' (with examples)

How to Use the Command 'navi' (with examples)

‘Navi’ is a powerful interactive cheatsheet tool for the command-line that aids users in efficiently obtaining and executing snippets and commands.

Read More
How to Use the Command 'blkdiscard' (with Examples)

How to Use the Command 'blkdiscard' (with Examples)

‘blkdiscard’ is a powerful command used in Linux systems to manage storage by discarding or “trimming” sectors on block devices, such as Solid State Drives (SSDs).

Read More