Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Using sed - Comprehensive Tutorial

Introduction to sed

The sed command, short for "stream editor," is a powerful utility in Unix and Unix-like operating systems for parsing and transforming text. It is commonly used for operations such as searching, find and replace, insertion, and deletion.

Basic Syntax

The basic syntax for using sed is:

sed [options] 'command' file

Here, command can be a sequence of operations that sed will perform on the input file.

Commonly Used Commands

Here are some common sed commands:

  • s/pattern/replacement/: Substitute the first occurrence of pattern with replacement in each line.
  • d: Delete the pattern space; immediately start the next cycle.
  • p: Print the pattern space.
  • y/source/destination/: Transliterate characters from source to destination.

Example: Basic Substitution

The following example demonstrates a basic substitution using the s command:

$ sed 's/Hello/Hi/' example.txt

This command replaces the first occurrence of "Hello" with "Hi" in each line of example.txt.

Input File (example.txt):
Hello, World!
Hello, sed!
Goodbye, sed!
Output:
Hi, World!
Hi, sed!
Goodbye, sed!

Example: Global Substitution

To replace all occurrences of a pattern in each line, add the g flag at the end of the s command:

$ sed 's/Hello/Hi/g' example.txt

This command replaces all occurrences of "Hello" with "Hi" in each line of example.txt.

Input File (example.txt):
Hello, World!
Hello, sed!
Goodbye, sed!
Output:
Hi, World!
Hi, sed!
Goodbye, sed!

Example: Deleting Lines

The following example demonstrates how to delete a line that matches a specific pattern:

$ sed '/Goodbye/d' example.txt

This command deletes any line containing "Goodbye" in example.txt.

Input File (example.txt):
Hello, World!
Hello, sed!
Goodbye, sed!
Output:
Hello, World!
Hello, sed!

Example: Inserting and Appending Lines

You can insert or append lines using the i and a commands:

$ sed '2i\Inserted Line' example.txt

This command inserts "Inserted Line" before the second line of example.txt.

$ sed '2a\Appended Line' example.txt

This command appends "Appended Line" after the second line of example.txt.

Input File (example.txt):
Hello, World!
Hello, sed!
Goodbye, sed!
Output for Insertion:
Hello, World!
Inserted Line
Hello, sed!
Goodbye, sed!
Output for Appending:
Hello, World!
Hello, sed!
Appended Line
Goodbye, sed!

Example: Using Multiple Commands

You can use multiple commands by separating them with a semicolon:

$ sed 's/Hello/Hi/; /Goodbye/d' example.txt

This command performs two operations: it replaces "Hello" with "Hi" and deletes lines containing "Goodbye".

Input File (example.txt):
Hello, World!
Hello, sed!
Goodbye, sed!
Output:
Hi, World!
Hi, sed!

Conclusion

This tutorial covered the basics of using sed for text processing and manipulation. There are many more advanced features and options available in sed, but with the basics covered in this guide, you should be able to start using sed effectively in your own tasks.