Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
CompTIA Linux+ XK0-005 - 3.1 - Common Script Utilities: sed
The sed
command, short for stream editor, is a powerful tool for performing text transformations in shell scripting. It allows you to modify and manipulate text files by applying a set of rules or commands to each line of input. With its versatile syntax and range of editing capabilities, sed
is widely used for tasks such as search and replace, line insertion and deletion, and text substitution.
Basic Syntax
The basic syntax of the sed
command is as follows:
-
sed OPTIONS 'COMMAND' FILE
OPTIONS
: Specifies any desired options for thesed
command.'COMMAND'
: Contains the set of editing commands to be applied to each line of input.FILE
: The name of the file to be processed. If not specified,sed
reads from standard input.
Common sed Options
sed
provides various options that modify its behavior and enable additional functionalities. Here are some commonly used options:
-
-n
: Suppresses the default output behavior ofsed
. Only explicitly specified commands with print instructions will produce output. -
-i
: Modifies the input file(s) in-place. This option allows you to directly edit the file(s) rather than writing the changes to standard output. -
-p
: Automatically prints each line of input after applying the specified commands. This option is useful for debugging or verifying the changes made bysed
.
Key Features and Functionality
The sed
command offers several key features that make it a valuable tool in shell scripting:
-
Search and Replace:
sed
allows you to search for a specific pattern or regular expression and replace it with another value. You can perform global replacements or limit replacements to specific occurrences or lines. -
Line Insertion and Deletion: With
sed
, you can add or remove lines from a file based on specified conditions. This includes inserting lines before or after a specific pattern, deleting lines that match a pattern, or removing empty lines. -
Selective Printing:
sed
allows you to selectively print lines from a file based on specified conditions. You can print lines that match a pattern, print a range of lines, or print specific columns.
Example Usage
Let's explore a few examples of how to use the sed
command:
Search and Replace
To replace all occurrences of a word in a file, you can use the following command:
sed 's/old_word/new_word/g' filename
This command searches for old_word
and replaces it with new_word
in the specified file.
Line Insertion
To insert a line before a specific pattern in a file, you can use the following command:
sed '/pattern/i new_line' filename
This command searches for the specified pattern
and inserts new_line
before it in the file.
Selective Printing
To print lines that match a pattern, you can use the following command:
sed -n '/pattern/p' filename
This command searches for the specified pattern
and prints only the lines that match it.
In-Place Editing
To edit a file in-place and save the changes, you can use the -i
option followed by an optional backup file extension:
sed -i.bak 's/pattern/replacement/' filename
This command replaces pattern
with replacement
in the specified file and creates a backup file with the .bak
extension.
Conclusion
The sed
command is a versatile tool for text editing and manipulation in shell scripting. It offers powerful features for search and replace, line insertion and deletion, and text substitution. By mastering the usage of sed
and understanding its options, you can efficiently process and transform text files in various scripting scenarios.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.