2 min read

A Beginner's Guide to Stream Editing with sed

Table of Contents

The sed command, short for stream editor, is a powerful utility for parsing and transforming text. It reads text line-by-line from a file or standard input, applies a specified command, and outputs the modified text. This makes it an essential tool for shell scripting and command-line data manipulation.

Basic Syntax

The basic structure of a sed command is:

sed 'command' [input-file]

Search and Replace

The most common use for sed is substitution. The s command is used for this.

To replace the first occurrence of “old” with “new” in each line of a file:

sed 's/old/new/' filename.txt

To replace all occurrences, add the g (global) flag:

sed 's/old/new/g' filename.txt

In-Place Editing

By default, sed only prints the modified text to the console. To save the changes back to the original file, use the -i option.

Caution: This will overwrite your file. It’s a good practice to test your command without -i first.

sed -i 's/old/new/g' filename.txt

You can also create a backup of the original file by providing a suffix to the -i option:

sed -i.bak 's/old/new/g' filename.txt

This creates filename.txt.bak before modifying the original.

Deleting Lines

The d command deletes lines.

To delete line 3:

sed '3d' filename.txt

To delete all lines containing “error”:

sed '/error/d' filename.txt

To delete empty lines:

sed '/^$/d' filename.txt

Conclusion

sed is a versatile tool with a wide range of commands and options. This guide covers the fundamentals to get you started. As you become more comfortable, you can explore its advanced features, like regular expressions and complex scripting, to handle even the most demanding text-processing tasks.