The sed command in Linux is a powerful text stream editor used for text manipulation and transformation. It operates by processing input text line by line and applying specified editing commands to modify the text. The sed command is particularly useful for performing tasks such as search and replace, deletion, insertion, and other text transformations.
The basic syntax of the sed command is:
sed [options] 'command(s)' input_fileWhere:
command(s)represent the editing commands used to modify the input text. Multiple commands can be separated by semicolons (;).input_fileis the file on which thesedcommands will be applied. If not provided,sedreads from standard input.
Here’s a table showing some common options of the sed command:
| Option | Description |
|---|---|
-n | Suppress automatic printing of pattern space. |
-e | Add a script of editing commands to the commands to be executed. |
-i | Edit files in place (changes the original file). |
--help | Display help information for the sed command. |
Examples:
- Search and replace text in a file:
sed 's/old_text/new_text/' input.txtThis command will replace the first occurrence of old_text with new_text in input.txt and print the result to the standard output.
- Search and replace all occurrences in a file (global substitution):
sed 's/old_text/new_text/g' input.txtThis command will replace all occurrences of old_text with new_text in input.txt.
- Delete lines matching a specific pattern in a file:
sed '/pattern/d' file.txtThis command will delete all lines containing the pattern from file.txt.
- Print only lines containing a specific pattern:
sed -n '/pattern/p' data.txtThis command will only print lines from data.txt that contain the pattern.
- Append text to the end of each line in a file:
sed 's/$/ - End of line/' content.txtThis command will append - End of line to the end of each line in content.txt.
- Edit files in place and create a backup file with a specific extension:
sed -i.bak 's/foo/bar/g' file.txtThis command will edit file.txt in place, making the substitution foo to bar in each occurrence. A backup of the original file will be created with the extension .bak.
- Display help information for the
sedcommand:
sed --helpThis command will provide detailed information on the various options available with the sed command.
The sed command is a versatile text manipulation tool and can be used for a wide range of text processing tasks. Its compact syntax and powerful capabilities make it a fundamental tool in the Unix/Linux command-line environment.