Sed Example

7 min read Oct 11, 2024
Sed Example

What is sed?

sed is a powerful command-line utility used in Unix-like operating systems like Linux and macOS for manipulating text streams. It stands for stream editor, and its primary function is to find and replace text within files or input streams.

Why Use sed?

  • Stream Processing: sed can process text streams, such as the output of other commands or even data from a network connection.
  • Non-Destructive: By default, sed edits a copy of the input, meaning it won't directly modify the original file unless you explicitly specify it.
  • Regular Expression Support: sed leverages regular expressions, allowing you to perform complex pattern matching and text manipulation.
  • Efficient: sed is designed to be efficient and handle large text files without significant performance bottlenecks.

Basic sed Syntax

The general syntax of a sed command is:

sed [options] 'command' [file...]
  • [options]: These specify how sed should operate, such as -i for in-place editing.
  • 'command': The command tells sed what to do with the input. Commands are enclosed in single quotes.
  • [file...]: Specifies the file(s) to be processed. If no file is specified, sed reads from standard input.

Common sed Commands

1. Substitution (s):

The s command is the workhorse of sed. It substitutes text based on a regular expression.

sed 's/old_text/new_text/g' file.txt
  • old_text: The text to be replaced.
  • new_text: The replacement text.
  • g: (Optional) Replace all occurrences on each line. Omitting g replaces only the first occurrence.

Example:

sed 's/apple/banana/g' fruits.txt

This command will replace all instances of "apple" with "banana" in the file "fruits.txt".

2. Deletion (d):

The d command deletes matching lines.

sed '/pattern/d' file.txt

Example:

sed '/error/d' log.txt

This command will delete all lines containing the word "error" from the "log.txt" file.

3. Insertion (i):

The i command inserts text before a matching line.

sed '/pattern/i\text to insert' file.txt

Example:

sed '/^chapter/i\This is a new chapter' book.txt

This command will insert the text "This is a new chapter" before every line starting with "chapter" in the "book.txt" file.

4. Appending (a):

The a command appends text after a matching line.

sed '/pattern/a\text to append' file.txt

Example:

sed '/^section/a\This is a new section' document.txt

This command will append the text "This is a new section" after every line starting with "section" in the "document.txt" file.

5. Changing (c):

The c command replaces matching lines with new text.

sed '/pattern/c\text to replace with' file.txt

Example:

sed '/^old_header/c\new_header' config.txt

This command will replace lines starting with "old_header" with "new_header" in the "config.txt" file.

Advanced sed Techniques

1. In-Place Editing:

To modify the original file directly, use the -i option:

sed -i 's/old_text/new_text/g' file.txt

2. Multiple Commands:

Use semicolons (;) to separate multiple commands within a single sed expression:

sed 's/old_text/new_text/g; s/another_old_text/another_new_text/g' file.txt

3. Backreferences:

Use \1, \2, etc. to refer to captured groups in a regular expression.

sed 's/\([a-z]\+\) \([a-z]\+\)/\2 \1/' file.txt

4. Addressing Lines:

Use line numbers or ranges to specify which lines to process:

# Process lines 10 to 20
sed '10,20s/old_text/new_text/g' file.txt

# Process lines starting from line 5
sed '5,$s/old_text/new_text/g' file.txt

5. Piping to sed:

You can pipe the output of another command to sed:

ls -l | sed 's/total //g'

This will list files and remove the "total" string from the output.

Example Scenarios

  • Replacing Text in a File:
    sed 's/old_password/new_password/g' config.txt
    
  • Deleting Comments from a Script:
    sed '/^#/d' script.py
    
  • Formatting Text:
    sed 's/\s\+/\t/g' data.csv
    
  • Extracting Data from a Log File:
    grep "error" log.txt | sed 's/.*error: //g'
    

Conclusion

sed is a powerful tool for manipulating text streams, making it a valuable command-line tool for tasks such as text replacement, data extraction, and script formatting. With its regular expression support, sed offers flexibility and efficiency, enabling you to automate text manipulation tasks effectively.

Featured Posts