The Linux sed Command Ultimate Guide

linux sed command

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

The Linux sed command (Stream Editor) command is perhaps the most versatile and strong tool in the Unix-based operating systems. With it, you can search, replace, remove, and otherwise manipulate text quickly within files or standard input streams.

Regardless of whether you are a new Linux user or a seasoned one, mastering Linux sed command can really help advance your productivity when working with text manipulation and automation. This tutorial will guide you through all the things you should know about sed, from the fundamentals to the advanced methods.

What is sed?

linux sed command

sed is an abbreviation for Stream Editor. Unlike a traditional text editor, sed works on streams of text and supports on-the-fly modification without the need to open a file in an interactive editor. This makes it very efficient for batch processing and automation purposes.

Some of the most important features of Linux sed command are:

  • Text searching and replacement
  • Deletion of certain lines
  • Insertion and appending
  • Extraction of certain portions of a file
  • Multiple editing rules applied simultaneously

Basic Syntax of sed

The overall syntax for the Linux sed command is:

sed [OPTIONS] 'COMMAND' file

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

  • OPTIONS: Adjust sed’s behavior (such as activating in-place editing).
  • COMMAND: Specifies what sed is supposed to do (substitution, delete, etc.).
  • file: The file to operate on (optional; sed can also accept input from stdin).

sed may also be used with pipelines:

echo "Hello World" | sed 's/World/Linux/'

This prints:

1. Replacing Text with sed

Basic Substitution (s command)

The most frequent usage of Linux sed command is replacing text using the s command:

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

This substitutes the first instance of old_text with new_text in every line of file.txt.

Example:

echo "I love Java" | sed 's/Java/Linux/'

Output:

Global Replacement (g flag)

By default, Linux sed command only substitutes the first match of a pattern in a line. To substitute all matches, use the g flag:

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

sed 's/foo/bar/g' file.txt

Example:

echo "foo foo foo" | sed 's/foo/bar/g'

Output:

Case-Insensitive Replacement (I flag)

To replace case-insensitively, use the I flag:

echo "Hello HELLO hello" | sed 's/hello/world/Ig'

Output:

2. Deleting Lines with sed

Delete a Specific Line

To delete a particular line (e.g., line 3):

sed '3d' file.txt

Delete Lines Matching a Pattern

To delete lines with a given word:

sed '/error/d' file.txt

Delete a Range of Lines

To delete several lines (e.g., lines 2 to 5):

sed '2,5d' file.txt

Delete Blank Lines

To delete all blank lines:

sed '/^$/d' file.txt

Inserting and Appending Text

Insert a Line Before a Specific Line (i command)

sed '3i This is a new line' file.txt

This inserts “This is a new line” before line 3.

Append a Line After a Specific Line (a command)

sed '3a This is an appended line' file.txt

This inserts “This is an appended line” after line 3.

4. Extracting and Displaying Text

Print Specific Lines

To print only line 5:

sed -n '5p' file.txt

To print lines 3 to 7:

sed -n '3,7p' file.txt

To print lines with “error”:

sed -n '/error/p' file.txt

Print Every 3rd Line

sed -n '0~3p' file.txt

5. Handling Multiple Commands

You can use multiple sed commands with ; or -e:

sed -e 's/foo/bar/g' -e '3d' file.txt

Or use {} for readability:

sed -e '
s/foo/bar/g
3d
' file.txt

6. In-Place Editing (-i flag)

By default, sed does not modify files in place. To edit files in-place:

sed -i 's/error/warning/g' file.txt

To save a copy of the original file:

sed -i.bak 's/error/warning/g' file.txt

7. Advanced sed Tricks

Replace Only on Specific Line

To replace “foo” with “bar” only on line 3:

sed '3s/foo/bar/' file.txt

Replace Only the Last Occurrence

sed ':a; $!N; s/foo/bar/; ta' file.txt

Swap Two Words

sed 's/(word1)\ (word2)/\2 \1/' file.txt

8. Combining sed with Other Commands

Replace Text in Multiple Files

sed -i 's/old/new/g' .txt Using find and sed Together bash Copy Edit find. -name ".txt" -exec sed -i 's/foo/bar/g' {} +

Extract Email Addresses from a File

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

Conclusion

The linux sed command is a very powerful text processing tool in Linux. Whether you’re doing simple find-and-replace or complicated text manipulation, sed can do it efficiently. Mastering sed will enable you to automate tedious tasks, scrub data, and manipulate text files like a pro.

FAQs

1. What is sed in Linux?

Linux sed (Stream Editor) is a command with great power used to process and edit text from files or stdin. It facilitates searching, substituting, inserting, deleting, and manipulating text in an effective manner.

2. How do I use sed?

sed processes line by line, performs specified commands (such as substitution or deletion), and prints the transformed text. It does not alter files by default unless -i is used.

3. How do I delete special characters from a file?

To delete all non-alphanumeric characters:
sed 's/[^a-zA-Z0-9 ]//g' file.txt

Shumail
Shumail is a skilled content writer specializing in web content and social media management, she simplifies complex ideas to engage diverse audiences. She specializes in article writing, copywriting, and guest posting. With a creative and results-driven approach, she brings fresh perspectives and attention to detail to every project, crafting impactful content strategies that drive success.
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!