Get 50% Discount Offer 26 Days

Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
Advanced sed Command Techniques in Linux: Replace, Edit, and Delete Text

The Linux sed command, short for stream editor, is one of the most powerful tools available for Text manipulation. Whether you want to search and replace Text, delete lines, or extract parts of a file, sed offers a variety of functions to handle Text processing in an efficient and non-interactive manner. This article will take you through everything you need to know about sed, from basic commands to advanced usage, including common issues and how to troubleshoot them.

Advanced sed Command Techniques

1. What is sed?

Sed command line tool that allows you to perform fundamental Text modifications on an input data stream (typically a file or input from a pipeline). Unlike Text editors like Vim or Nano, which require manual interaction, sed can automate Text processing tasks. It’s widely used for:

  • Searching for specific Text patterns
  • Replacing Text within files
  • Deleting lines from files
  • Modifying Text based on patterns

The best part? You don’t need to open the file in an editor to make changes—sed can do it all from the command line.

2. Basic Syntax of sed

Let’s first understand the basic syntax of sed Before diving into examples:

sed [options] 'command' file
  • Options: Flags that modify the behavior of sed.
  • Command: The action you want to perform (like substitution, deletion, etc.).
  • File: The file you wish to process.

Options in sed

Here are some useful options you will frequently use with sed:

  • -n: Suppresses automatic printing of lines. Those lines which are modified by commands will be printed.
  • -i: Edits the file in-place, saving changes directly to the file.
  • -e: Allows you to run multiple sed commands in sequence.

3. Everyday Use Cases for sed

Let’s start with some simple examples that illustrate the power of sed for everyday text-processing tasks.

3.1. Replacing Text in a File

One of the most common uses of sed is to replace Text. The s (substitute) command allows you to replace occurrences of one string with another.

Syntax:

sed 's/old-text/new-text/' filename
Replacing Text in a File using sed Command

Example: Replace the word “apple” with “orange” in a file:

sed 's/apple/orange/' fruits.txt
Replace the word apple with orange in a file

This action replaces the first occurrence of the word ‘apple’ on each line with ‘orange.’

Replace All Occurrences in a Line

To replace every occurrence of the word on a line, add the g (global) flag:

sed 's/apple/orange/g' fruits.txt
replace every occurrence of the word on a line

Replace Text on Specific Lines

You can also target a specific line number. For example, to replace the word “apple” with “orange” only on the second line:

sed '2s/apple/orange/' fruits.txt
Replace Text on Specific Lines 1

3.2. Deleting Lines

sed can delete lines based on line numbers or patterns.

Syntax:

sed 'Nd' filename
Deleting Lines
  • N: The line number you want to delete.

Example: To delete the third line from a file:

sed '3d' file.txt
To delete the third line from a file

Deleting a Range of Lines

You can also delete a range of lines:

sed '3,5d' file.txt
Deleting a Range of Lines

This command deletes lines from 3 to 5.

Deleting Lines Matching a Pattern

Then, you can remove lines that have the same pattern using this command:

sed '/pattern/d' file.txt
Deleting Lines Matching a Pattern

Example: Delete all lines containing the word “error”:

sed '/error/d' logfile.txt
Delete all lines containing the word error

3.3. Inserting and Appending Text

Using the i (insert) and a (append) commands, you can insert or append Text at a specific location in a file.

Insert Text Before a Line:

sed '2i\Inserted line' file.txt
Insert or append text at a specific location in a file

Append Text After a Line:

sed '2a\Appended line' file.txt
Append Text After a Line

Enter or add Text to the line numbers in order to change the file values.

4. Advanced sed Techniques

After you’ve learned the rudiments, you will crave knowledge of what more sed has to offer. Included are patching regular expressions, conditional replacements, and combining multiple commands.

4.1. Using Regular Expressions (Regex)

sed is capable of understanding and using regular expressions, which allows for more complex Text manipulation.

Example: Replace any sequence of digits with the word “number”:

sed 's/[0-9]\+/number/g' file.txt
sed is capable of understanding and using regular expressions

This operation replaces all digit sequences in the file with the word ‘number.’

4.2. Performing Multiple Operations

You can perform multiple operations in one sed command by separating them with a semicolon (;):

sed 's/apple/orange/g; 5d' file.txt
perform multiple operations in one sed command

This command replaces “apple” with “orange” globally and then deletes the fifth line.

4.3. Save Changes to the File

By default, sed works on the input file without making any changes to the original file, ensuring the original stays intact. However, you can use the -i flag to edit the file in place:

sed -i 's/old-text/new-text/g' file.txt
Save Changes to the File

“This action saves the changes directly to the file without creating a new copy.”

Creating a Backup File

When using the—i option, you can specify a backup extension for the original file and have a copy of it as a backup.

sed -i.bak 's/old-text/new-text/g' file.txt
create a backup of the original file by adding a backup extension

This process creates an original copy of the file by adding a .bak extension before making any changes.

Sed is definitely a versatile tool, but users may need help using it. Here’s how to resolve them:

5.1. Problem: sed Not Changing Anything

Solution: Make sure you don’t just print the output but actually modify the file.

sed -i 's/apple/orange/g' file.txt
sed Not Changing Anything

5.2. Problem: Special Characters Causing Errors

Solution: Some characters like /, &, and \ are special in sed and need to be escaped with a backslash.

Example: To replace /home/user with /root:

sed 's/\/home\/user/\/root/' file.txt
Special Characters Causing Errors

Alternatively, you can use different delimiters:

sed 's|/home/user|/root|' file.txt
you can use different delimiters

5.3. Problem: Blank Lines Not Being Deleted

Solution: Ensure you’re using the correct pattern to match blank lines. Use the following command to delete all blank lines:

sed '/^$/d' file.txt
Blank Lines Not Being Deleted

5.4. Problem: sed Hanging or Freezing

Solution: It is usually caused when sed is trying to read from a very huge file. If you need to optimize your sed commands and you want to divide the file into pieces in order.

6. Summary of Important Commands

Here’s a quick overview of the most commonly used sed commands:

CommandDescription
sed ‘s/old/new/’ fileReplace the first occurrence of “old.”
sed ‘s/old/new/g’ fileReplace all occurrences of “old” globally
sed ‘Nd’ fileDelete line number N
sed ‘/pattern/d’ fileDelete lines matching a pattern
sed -n ‘Np’ filePrint line number N
sed -i ‘s/old/new/g’ fileEdit the file in place
sed ‘Ni\new line’ fileAppend Text after line N
sed ‘Na\new line’ fileAppend text after line N
sed ‘/^$/d’ fileDelete all blank lines

Conclusion

Linux Text Processing is just not a process, but sed is a cornerstone. Small files, large files, whether used for repetitive tasks working with only a little bit of file or an awful lot of file, sed is your friend here. From slight Text substitution to advanced usage, like using regular expressions and solving an everyday problem, we’ve learned everything.

By mastering sed, you’ll be able to manipulate Text files quickly and precisely, making it invaluable for system administrators, developers, and anyone working with Text files in Linux.

About the writer

Vinayak Baranwal Article Author

Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.

Leave a Reply

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

Lifetime Solutions:

VPS SSD

Lifetime Hosting

Lifetime Dedicated Servers