Sed Like a Pro: A Beginner to Advanced Tutorial for Linux Users

MONI
3 min readSep 17, 2024

--

Introduction

If you’re looking to manipulate text in Linux, the sed command is one of the most powerful tools you can learn. Short for Stream Editor, sed allows you to search, replace, insert, and delete text—all from the command line. In this tutorial, we’ll go from the basics of sed to more advanced usage, complete with examples, tricks, and exercises to help you master this essential tool.

What is sed?

sed, short for Stream Editor, is a Unix utility used to filter and transform text. It reads input from a file or standard input, makes the specified modifications, and outputs the result. The great thing about sed is that it doesn’t modify the original file unless you explicitly save the output.

Think of sed as a non-interactive text editor that can be scripted or run directly from the command line to automate text transformations.

Basic Syntax

The basic syntax of sed looks like this:

sed [options] 'command' file
  • [options]: Additional flags you can use with sed, such as -i to edit the file in place.
  • 'command': The transformation you want to apply.
  • file: The file on which you want to apply the command.

Example 1: Simple Search and Replace

The most common use of sed is for searching and replacing text in a file. Let’s say you have a file called example.txt containing the following text:

Hello world

You can replace the word “world” with “Linux” using sed:

sed 's/world/Linux/' example.txt

This will output:

Hello Linux

The s command stands for "substitute," and it follows the pattern:

sed 's/old/new/'

Common Use Cases

1. Search and Replace

You can use sed to find and replace text in a file:

sed 's/old_text/new_text/' file.txt

If you want to replace all occurrences of a word in a file:

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

The g flag at the end means "global," replacing every instance of old_text in the line.

2. Deleting Lines

You can also delete lines using sed. For instance, to delete line 3 from a file:

sed '3d' file.txt

To delete a range of lines (e.g., lines 2 through 4):

sed '2,4d' file.txt

3. Inserting or Appending Text

To insert text before a specific line, use the i command:

sed '2i\This is a new line' file.txt

To append text after a specific line, use the a command:

sed '2a\This is an appended line' file.txt

Advanced sed Features

1. Using Regular Expressions

sed becomes really powerful when combined with regular expressions. For example, to replace all digits in a file with the word "number":

sed 's/[0-9]/number/g' file.txt

You can also use word boundaries to make more precise replacements. To replace the word “is” only when it appears as a whole word, not part of another word:

sed 's/\bis\b/was/g' file.txt

2. Working with Ranges

You can apply commands to a range of lines using sed. For example, to replace text only between lines 5 and 10:

sed '5,10s/old/new/g' file.txt

3. Using Multiple Commands

If you want to run multiple sed commands in one go, you can either chain them together using the -e option or place them inside curly braces {}.

sed -e 's/old/new/' -e 's/foo/bar/' file.txt

Or :

sed '{ s/old/new/; s/foo/bar/ }' file.txt

Tricks and Tips

  • Use -i for In-Place Edits: If you want to modify the file directly, use the -i option:
sed -i 's/old/new/g' file.txt
  • Combine with Pipes: sed works well in combination with other commands. For example, using sed with grep:
grep "pattern" file.txt | sed 's/pattern/replacement/'
  • Escape Special Characters: If you need to replace characters like /, use a backslash \ to escape them:
sed 's/\/old\/text/\/new\/text/' file.txt

Practice Exercises

  1. Basic Replacement: Replace all instances of “apple” with “orange” in a file.
  2. Delete a Range of Lines: Delete lines 5 through 10 from a file.
  3. Insert Text Before a Line: Insert the line “This is inserted” before line 3 in a file.
  4. Use Regular Expressions: Replace all numbers with the word “digit” in a file.
  5. Multiple Commands: Replace “cat” with “dog” and “mouse” with “rat” in the same file.

Conclusion

With these examples and exercises, you’ve just scratched the surface of what sed can do. From basic text replacement to advanced transformations using regular expressions, sed is an incredibly flexible tool that every Linux user should master. Keep practicing with different files and commands, and soon you'll be a sed expert!

Feel free to leave any comments or questions below if you need further clarification on anything in this tutorial!

--

--

MONI

Programming is all about mindset, and CyberSecurity is a process that includes Ethical Hacking. Currently, I'm diving deep into Rust.