Categories
shellinfo tips

SED – basic text transformations on files or pipelines

sed is a stream editor for filtering and transforming text. A stream editor is used to perform basic text transformations on an input stream – a file or input from a pipeline. Sed works by executing commands, called sed scripts, on an input stream.

How to Use Sed

sed is invoked using the following syntax:

sed [options] 'command' inputfile

Where options is one or more sed options, command is a sed command, and inputfile is the name of the file to be processed by the sed command.

Sed Options

sed provides a number of options for controlling its behavior. Some of the more commonly used options are listed below.

Option Description
-n Suppress automatic printing of pattern space
-e Execute the script provided as the next argument
-f Execute the script provided in the file specified as the next argument

Sed Commands

sed provides a number of commands for performing text transformations. Some of the more commonly used commands are listed below.

Command Description
s/pattern/replacement/ Replace the first occurrence of pattern with replacement
s/pattern/replacement/g Replace all occurrences of pattern with replacement
y/pattern/replacement/ Transliterate the first occurrence of pattern with replacement
y/pattern/replacement/g Transliterate all occurrences of pattern with replacement
d Delete the pattern space and start next cycle

Sed Examples

The following examples demonstrate some of the ways in which sed can be used.

Example 1: Replace a String

In the following example, the sed command will replace the string oldstring with the string newstring.

sed 's/oldstring/newstring/' inputfile

Example 2: Replace a String (Global)

In the following example, the sed command will replace all occurrences of the string oldstring with the string newstring.

sed 's/oldstring/newstring/g' inputfile

Example 3: Transliterate a String

In the following example, the sed command will transliterate the string oldstring to the string newstring.

sed 'y/oldstring/newstring/' inputfile

Example 4: Transliterate a String (Global)

In the following example, the sed command will transliterate all occurrences of the string oldstring to the string newstring.

sed 'y/oldstring/newstring/g' inputfile

Example 5: Delete a Line

In the following example, the sed command will delete all lines that contain the string oldstring.

sed '/oldstring/d' inputfile

Example 6: Insert a Line

In the following example, the sed command will insert the string newstring on a new line after every line that contains the string oldstring.

sed '/oldstring/a\newstring' inputfile

Example 7: Insert a Line (Before)

In the following example, the sed command will insert the string newstring on a new line before every line that contains the string oldstring.

sed '/oldstring/i\newstring' inputfile

Example 8: Substitute on a Specific Line

In the following example, the sed command will replace the string oldstring with the string newstring on line 5 of the input file.

sed '5s/oldstring/newstring/' inputfile

Example 9: Substitute on a Range of Lines

In the following example, the sed command will replace the string oldstring with the string newstring on lines 5 through 10 of the input file.

sed '5,10s/oldstring/newstring/' inputfile

Example 10: Print Only Matching Lines

In the following example, the sed command will print only those lines that contain the string oldstring.

sed -n '/oldstring/p' inputfile

Example 11: Print Only Non-Matching Lines

In the following example, the sed command will print only those lines that do not contain the string oldstring.

sed -n '/oldstring/!p' inputfile

Example 12: Invert Matching Lines

In the following example, the sed command will print only those lines that do not contain the string oldstring.

sed '/oldstring/!p' inputfile

Example 13: Delete Blank Lines

In the following example, the sed command will delete all blank lines in the input file.

sed '/^$/d' inputfile

Example 14: Substitute on a Pattern Match

In the following example, the sed command will replace the string oldstring with the string newstring on those lines that contain the string pattern.

sed '/pattern/s/oldstring/newstring/' inputfile

Example 15: Delete on a Pattern Match

In the following example, the sed command will delete those lines that contain the string pattern.

sed '/pattern/d' inputfile

Leave a Reply

Your email address will not be published. Required fields are marked *