A Complete Guide on How to Use sed (Stream Editor) in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

Linux sed Command_ How To Use the Stream Editor

sed stands for stream editor in Linux, a powerful command-line tool in Linux for parsing and transforming text. Think of it as a friendly text editor that reads through your words and efficiently processes textual data and configuration files. 

With simple commands, you tell sed what to find in your text and what to replace it with. It’s a quick and easy way to replace multiple patterns without hassle. 

Check out our quick tutorial on using sed to find and replace a string in a file – perfect for those short on time!

In this detailed version, we’ll discuss the basic sed syntax and then go into several use cases where you can use it to find and replace text based on several parameters.

Table Of Contents

  1. Understanding Linux sed Syntax
  2. Linux sed Options
  3. Practical Examples to Explore sed Commands
    1. Example #1: Replace a String
    2. Example #2: Replace All Occurrences of a String
    3. Example #3: Replace a Specific Occurrence in a Line Using the sed Command
    4. Example #4: Only Print Lines With Substitute Text
    5. Example #5: Replace String Using the sed Command and Ignore Case
    6. Example #6: Replace String in Specific Line Using the sed Command
    7. Example #7: Replace String Only in a Specific Range of Lines
    8. Example #8: Delete a Specific Line
    9. Example #9: Delete Lines Within a Specific Range
    10. Example #10: Delete From Specific to the Final Line
  4. sed Expressions and Patterns
    1. Default Actions and Output Stream
    2. Handling Blank Lines and Line Output
    3. Handling Special Characters and Clear Text Forms
    4. Working with a Single Address
    5. Command Options and Temporary Files
  5. Conclusion
  6. FAQs

Understanding Linux sed Syntax

The Linux sed command follows a specific syntax pattern, which includes various options, a script (if needed), and an input file (optional): 

# sed [options] [script] [input_file...]

Let’s break down the syntax to understand it better:

Options: The purpose of options is to fine-tune sed behavior. For instance, -n suppresses automatic printing.

Script: You can pass a script containing multiple instructions as a parameter

Input_File: The input file you wish to process with sed.

Linux sed Options

Command line flags are critical to fine-tuning and optimizing how sed access, processes, and outputs the processed text. Here’s an overview of sed command-line options.

Short Option Long Option Description
-b – -binary Opens input files in binary mode, treating lines as ending at a line feed.
– -debug Activates debug mode for canonical input printing and program execution annotation.
– -follow-symlinks Enables editing the final destination of symbolic links when used with -i.
– -help Displays usage information to assist users in understanding sed’s functionality.
– -i – -in-place [=SUFFIX] Enables in-place edits by overwriting the original file; optional SUFFIX for backups.
– -posix Turns off all extensions to POSIX sed, simplifying the writing of portable scripts.
– -version Displays the version of the sed utility currently running on your system.
-E, -r – -regexp-extended Allows extended regular expressions, enhancing pattern-matching capabilities.
-e script – -expression=script Adds a specific script to run alongside the sed commands, offering flexibility in text manipulation.
– -f -file=script-file Incorporates a script file’s contents to run with the sed commands.
-l N – -line-length=N Defines the desired line-wrap length for the ‘l‘ command, defaulting to 70.
-n – -quiet, – -silent Disables output printing, allowing for silent text processing.
-s – -separate Views specified files as separate entities when dealing with multiple files.
– -sandbox Turns off the execution of external programs, focusing solely on manipulating input files.
-u – -unbuffered Minimizes input and output buffering for efficient processing.
-z – -null-data,

– -zero-terminated

Interprets input as a series of null-terminated lines, enabling unique text processing.

Understanding and utilizing these options allows you to fully leverage the sed command for various text editing tasks in Linux.

Practical Examples to Explore sed Commands

Now that you have a good idea about the basic sed syntax, we’ll explore ten essential sed commands and demonstrate them with easy-to-follow examples. 

For this demonstration, we’ll use a sample file called aliceinwonderland.txt with the following text:

Alice in wonderland.

Rabbit in a top hat.

Alice chasing the rabbit in wonderland.

Tea party with the Mad Hatter.

The Queen of Hearts in her palace.

Remember that, by default, sed won’t change the original file unless you use the -i option. It displays the modifications and processed text in the standard output. We strongly recommend checking this output before you use the -i flag to make changes to the original text. 

Now, let’s explore these essential sed commands:

Example #1: Replace a String 

The sed command for replacing strings changes specific text patterns in a file. It allows you to efficiently update text within a file, making it handy for quick edits or substitutions. 

Let’s see its syntax:

# sed 's/old_string/new_string/' aliceinwonderland.txt

Here:

  • s: signifies the substitution operation.
  • old_string: The text you want to replace.
  • new_string: The replacement text.
  • filename: The file containing the text to modify. In this case, it is aliceinwonderland.txt.

Let’s see an example of the syntax below:

# sed 's/Rabbit/Cheshire Cat/' aliceinwonderland.txt

This command replaces the first occurrence of Rabbit with Cheshire Cat in the file.

rabbit cheshire cat alice in wonderland

Example #2: Replace All Occurrences of a String

The sed can globally substitute a specific text pattern throughout a file. It efficiently updates multiple instances of a text pattern within the file.

The command to replace every occurrence of a specified string in a text is:

# sed 's/old_string/new_string/g' aliceinwonderland.txt

Here, the g in syntax stands for global, indicating that the utility will replace all occurrences in each line.

Here’s an example:

# sed 's/Alice/Alice in Wonderland/g' aliceinwonderland.txt

This command replaces all occurrences of Alice with Alice in Wonderland in the file.

alice in wonderland

Example #3: Replace a Specific Occurrence in a Line Using the sed Command

sed substitutes the targeted text pattern within a line of a file to replace a specific occurrence in a line. It allows for precise replacements within specific lines, providing control over the editing process. The syntax is as follows:

# sed 's/old_string/new_string/#' aliceinwonderland.txt

Here, the # indicates the specific occurrence you want to target within each line.

For instance, if you want to replace the second occurrence of in with inside in each line, use this command:

# sed 's/in/inside/2' aliceinwonderland.txt

The output of this command shows the replacement of only the second occurrence of the word in within each line, as shown below:

inside 2 alice in wonderland

Example #4: Only Print Lines With Substitute Text

You can use the following sed command to print only the lines with substitute text. It helps filter lines in large files and highlights the lines containing replacement texts. The syntax of the command is:

# sed -n 's/old_string/new_string/p' aliceinwonderland.txt

To disable automatic printing, use the -n option and p to print substituted lines.

For instance, to replace wonderland with enchanted land and display only the lines with the substitution, use this command:

# sed -n 's/wonderland/enchanted land/p' aliceinwonderland.txt

Output:

enchanted land

Example #5: Replace String Using the sed Command and Ignore Case

The sed command for replacing a string with case insensitivity is used to substitute a specific text pattern throughout a file, ignoring the letter case. It enables text replacement regardless of the uppercase or lowercase variations, providing flexibility in text edits.

The syntax of the command is:

# sed 's/old_string/new_string/i' aliceinwonderland.txt

Where the I enables case insensitivity.

So, to replace both uppercase and lowercase instances of rabbit with Cheshire Cat in the text, use this command:

# sed 's/rabbit/Cheshire Cat/i' aliceinwonderland.txt

The output of the command is:

rabbit cheshire cat

In this output, you can see that rabbit has been changed in all instances, ignoring the letter case, thanks to the sed’s case-insensitive mode.

Example #6: Replace String in Specific Line Using the sed Command

The sed command for changing a string in a specific line lets you swap out a particular text in just one line. It allows for precise replacements within a line, providing better control over the editing process. The typical syntax is:

# sed 'Ls/old_string/new_string/' filename

Here, L specifies the line number where substitution should occur.

For instance:

# sed '3 s/Alice/Dorothy/' aliceinwonderland.txt

The output shows that the first appearance of Alice in the second line stays the same, but in the third line, it has been replaced by Dorothy instead.

sed 3 alice in wonderland

Example #7: Replace String Only in a Specific Range of Lines

If you wish to replace multiple string occurrences within a defined line range without affecting the entire text, you can use the following sed command:

# sed '#,# s/old_string/new_string/' aliceinwonderland.txt

In this syntax, replace the first # with the starting line number and the second # with the final line number you want in the range.

For instance, to replace the last two instances of Alice with Dorothy in lines 3 to 5, use the following command:

# sed '3,5 s/Alice/Dorothy/' aliceinwonderland.txt

This output clearly shows that Alice, within the specified line range, has changed to Dorothy, maintaining the rest of the text in the file.

sed 3,5 alice in wonderland

Example #8: Delete a Specific Line

The sed command for deleting a specific line removes unwanted text by targeting a line in a file. Its syntax is:

# sed '#d' aliceinwonderland.txt

In this syntax, replace the # with the line number you wish to remove from the file.

For instance, to remove the 3rd line from the file, use the following command:

# sed '3d' aliceinwonderland.txt

Output:

output alice in wonderland

As you can see in the following output, the third line (Dorothy chasing the rabbit in wonderland) has been deleted.

Example #9: Delete Lines Within a Specific Range

Use the following sed command to delete lines within a specified line range:

# sed '#,#d' aliceinwonderland.txt

Replace ‘the first # with the beginning line number and the next # with the last number in the range. 

For instance, if you want to delete lines 2 to 4 from the file, use the following command:

# sed '2,4d' aliceinwonderland.txt

The output will confirm the successful removal of the specified lines, preserving the remaining text.

cat alice wonderland

Example #10: Delete From Specific to the Final Line

To delete lines starting from a specific line number to the last line of a file, use the following delete command:

# sed '#,$d' aliceinwonderland.txt

Replace the # symbol with the line number from which you want to start deleting, and execute the command.

For instance, to delete everything from the third line to the end of the file, use this command:

# sed '3,$d' aliceinwonderland.txt

The output shows that the file now contains two lines of text:

sed 3 alice in wonderland

sed Expressions and Patterns

Expressions and patterns are the rules and criteria sed uses to find and manipulate text in files. When using sed, expressions define what text to find, and patterns describe the structure/format of that text. 

Let’s look at these ideas in some detail:

Default Actions and Output Stream

By default, sed reads a line, performs specified actions or expressions, and then sends the results to the standard output stream. 

A typical syntax is:

# sed 's/old_string/new_string/' input.txt > output.txt

This command takes input from input.txt, swaps old_string with new_string, and displays the modified text in output.txt.

Handling Blank Lines and Line Output

Ever wonder how to deal with spaces in your text that seem to do nothing? 

sed is an excellent tool for managing blank lines and their impact. You can use the -N option to work with empty lines or lines with specific patterns. 

Handling Special Characters and Clear Text Forms

sed simplifies dealing with tricky character patterns by keeping them organized. We suggest using the -F flag to ensure these characters behave as intended. This simplifies text edits and makes the entire process straightforward and hassle-free. 

The basic syntax of a sed command looks like this:

# sed 's/old_string/new_string/' input.txt

However, if the old_string contains special characters, using the basic form might lead to unexpected behavior. You can use the -F flag to work with special characters in target strings:

# sed -F 's/old_string/new_string/' input.txt

This flag instructs sed to treat old_string as a plain text string, allowing you to manipulate it as you wish.

Working with a Single Address

sed makes it simple to choose precisely which lines the command will act upon. In this context, an address can be a line number or a pattern. For instance, to alter only line 5, you can use:

# sed '5s/old_string/new_string/' input.txt

This command exclusively switches old_string to new_string on the 5th line of input.txt. 

Command Options and Temporary Files

You can customize specific sed actions with command options. For instance, when editing files in place, you can use the -i option to create a file’s backup copy.

# sed -i.bak 's/old_string/new_string/' input.txt

In this example, the original file is edited in place, and a backup copy with a .bak extension is created.

Conclusion

sed is a popular command-line text editor for streamlining and automating text transformations. With its versatile text manipulation capabilities, it can save countless hours of manual editing. 

If you’re looking for a robust server for your projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the sed stream editor?

The sed stream editor is a powerful tool in Linux for performing fundamental text transformations. It operates line-by-line and can modify, delete, or insert text in a text stream.

Q. How is sed different from a regular text editor?

Unlike regular text editors, sed is a command-line tool that allows you to process large amounts of text in an automated way. It doesn’t provide an interactive interface like text editors like vi or nano. Still, it takes input from an input stream and applies various basic commands to manipulate the text.

Q. What is the syntax for using the sed stream editor?

The basic syntax for sed is:

# sed [options] [script] [input_file...]

Q. What does the ‘g’ in the sed command stand for?

The ‘g‘ stands for “global”. It replaces all occurrences of a pattern match in a line.

Q. Can I use sed to edit files directly?

Yes, by using the -i option, you can edit files in place without redirection.

Q. Is sed exclusive to Linux?

No, sed is available on most Unix-like systems, including macOS.

Q. How do I modify text using sed?

To modify text using sed, you can use the substitution command. The syntax for the substitution command is:

s/find/replace/

For instance, to replace all occurrences of “old_string” with “new_string” in a text stream, you can use the following command:

# sed 's/old_string/new_string/' input.txt

Q. Can sed perform multiple operations on a text stream?

Yes, sed offers a variety of commands that can be combined to perform multiple operations on a text stream. By using the “-e” option followed by multiple sed commands, you can execute them one after the other. For instance:

# sed -e 's/old_string/new_string/' -e 's/old_string/new_string/' input.txt

Q. How can I apply sed commands to a specific range of lines?

To apply sed commands to a specific range of lines, you can use the address range feature of sed. The syntax for specifying an address range is:

sed '#,# s/old_string/new_string/' input.txt

Q. Can I write a sed script in a file and execute it?

Yes, you can write a sed script in a file and execute it using the “-f” option followed by the file name. This allows you to reuse the sed script for different input files. For instance:

sed -f script.sed input.txt۔

Try this guide with our instant dedicated server for as low as 40 Euros