What is sed Linux command?
The sed command in Linux is a versatile stream editor used for text manipulation tasks. It can search, find and replace, insert, and delete text in files quickly and efficiently. Sed is a valuable tool for automating tasks and editing text in scripts and command-line operations.
sed Syntax:
sed Options:
Option | Description |
---|---|
-e | Add the script to the commands to be executed. |
-i | Edit files in place (make changes directly in the file). |
-n | Suppress automatic printing of pattern space. |
-r | Use extended regular expressions in the script. |
Parameters:
Parameter | Description |
---|---|
script | The script to be executed on each line of the input file. |
filename | The file to be processed by the sed command. |
sed Command Usage Examples:
Replace a Specific Word in a File
This command replaces every occurrence of “original_word” with “replacement_word” in the file “file.txt”.
Delete Empty Lines in a File
This command removes all empty lines from the file “file.txt”.
Substitute Text Using a Regular Expression
This command replaces the last two digits of each line in the file “file.txt” with “XY”.
Print Lines Between Two Patterns
This command prints all lines between “start_pattern” and “end_pattern” from the file “file.txt”.
Append Text at the End of Each Line
This command adds ” - added text” at the end of each line in the file “file.txt”.
How do I perform a basic search and replace operation with sed in Linux?
To perform a basic search and replace operation using sed in Linux, you can use the following command:
How do I delete specific lines from a file using sed in Linux?
To delete specific lines from a file using sed in Linux, you can use the following command:
How do I append a line after a specific pattern in a file using sed in Linux?
To append a line after a specific pattern in a file using sed in Linux, you can use the following command:
How do I replace text in a specific line using sed in Linux?
To replace text in a specific line using sed in Linux, you can use the following command:
How do I perform a case-insensitive search and replace operation with sed in Linux?
To perform a case-insensitive search and replace operation using sed in Linux, you can use the following command:
How do I save the changes made by sed directly to the original file in Linux?
To save the changes made by sed directly to the original file in Linux, you can use the following command:
How do I perform a search and replace operation only on lines matching a specific pattern with sed in Linux?
To perform a search and replace operation only on lines matching a specific pattern using sed in Linux, you can use the following command:
How do I substitute a specific occurrence of a pattern in a line using sed in Linux?
To substitute a specific occurrence of a pattern in a line using sed in Linux, you can use the following command:
Applications of the sed command
- Search and replace text in files
- Text substitution
- Remove lines from a file
- Insert text at specific line numbers
- Text pattern matching and removal
- Stream editing
- Text transformation
- Batch processing of files
- File content manipulation