Skip to main content
Text Manipulation:

GNU sed recipes

Summary

GNU sed (stream editor) is a powerful text processing tool commonly used for parsing, modifying, and transforming text streams in Unix-based systems. This article provides a collection of useful command examples and sed recipes with detailed explanations and examples.

What is sed? #

GNU sed, or the stream editor, is a command-line text processing tool that operates on streams of text input. It reads text from a specified source, applies specified editing commands, and then outputs the modified text.

sed is a powerful and versatile tool for text manipulation in Unix-like environments. Check the GNU sed cheat sheet for even more sed command examples.

Basic usage #

The general syntax of sed is:

sed [options] 'command' file

To print only a specific line from a file:

sed -n '5p' file.txt

This prints the 5th line from file.txt.

To print a range of lines (e.g., lines 5 to 10):

sed -n '5,10p' file.txt

Substitutions #

Basic find and replace #

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

This replaces the first occurrence of “old” with “new” on each line.

Global replacement #

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

This replaces all occurrences of “old” with “new” on each line.

Case-insensitive replacement #

sed 's/old/new/gi' file.txt

The i flag makes the substitution case-insensitive.

Replace in a specific line #

sed '5s/old/new/' file.txt

This replaces “old” with “new” only in the 5th line.

Deleting lines #

sed recipes for removing/deleting lines from a file following one or more expressions, invoked by the -e / --expression= operator.

sed does not remove the lines from the source file by default. Use the -i option with the sed command to work on the source file. Alternatively, use redirection to save the output to a new file, e.g. sed '1d' file > newfile.

Delete a specific line #

sed '5d' file.txt

Deletes the 5th line from the file.

Delete a range of lines #

sed '5,10d' file.txt

Deletes lines from 5 to 10.

Delete empty lines #

sed '/^$/d' file.txt

Removes all empty lines.

Delete lines that begin with a specified character #

sed '/^a/d' file.txt

Deletes lines that begin with “a”.

Delete lines that end with a specified character #

sed '/a$/d' file.txt

Deletes lines that end with “a”.

Delete lines where contents are written in upper case #

sed '/^[A-Z]*$/d' file.txt

Deletes lines that contain only uppercase letters.

Delete lines that contain a specific pattern #

sed '/foobar/d' file.txt

Deletes lines that contain the pattern “foobar”.

Delete lines starting from a pattern till the last line #

sed '/foobar/,$d' file.txt

Deletes lines starting from “foobar” to the last line.

Inserting and appending text #

Insert a line before a specific line #

sed '5i\New line inserted' file.txt

Inserts “New line inserted” before line 5.

Append a line after a specific line #

sed '5a\New line appended' file.txt

Appends “New line appended” after line 5.

Modifying lines #

Change a specific line #

sed '5c\This is the new content' file.txt

Replaces the entire 5th line with “This is the new content”.

Replace text in multiple files #

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

The -i option modifies files in place. This replaces “old” with “new” in all .txt files.

Advanced patterns #

Replace only if a pattern matches #

sed '/pattern/s/old/new/' file.txt

Replaces “old” with “new” only in lines that contain “pattern”.

Replace using regular expressions #

sed -E 's/[0-9]+/NUMBER/g' file.txt

This replaces all numeric sequences with “NUMBER”.

Extracting parts of a file #

Extract all email addresses:

sed -nE 's/.*([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}).*/\1/p' file.txt

Working with delimiters #

If your pattern contains /, use an alternative delimiter:

sed 's|/usr/local/bin|/opt/bin|g' file.txt

Using | instead of / avoids confusion.

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

GNU sed recipes by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/sed-recipes">GNU sed recipes</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the Ditig legal page.

All Topics

Random Quote

“Obey standards unless you've a darn good reason.”

Alan Cooper  Software designer and programmerAbout Face, - IT quotes