What is the sed Command in Linux?
The sed
(stream editor) command in Linux is a powerful tool used for text manipulation. It can perform various text operations, such as substitution, deletion, and insertion, on files or streams of data. It is often used for automation in shell scripts to modify files or input without needing to open an editor.
Basic syntax:
sed [options] 'command' file
Replace a string in a file:
sed 's/old_string/new_string/' file.txt
Options
Option | Description |
---|---|
-n | Suppresses automatic output, useful when you only want specific lines |
-e | Allows multiple commands to be executed |
-i | Modifies the file in place (saves changes directly to the file) |
-r | Enables extended regular expressions in the script |
Common Use Cases
1. Substitution
The most common operation in sed
is substitution, which replaces one string with another:
sed 's/old/new/' file.txt
To replace all occurrences on a line, use the g
(global) flag:
sed 's/old/new/g' file.txt
2. Delete Lines
You can delete specific lines from a file:
sed '2d' file.txt # Deletes line 2
sed '5,10d' file.txt # Deletes lines 5 through 10
3. Inserting or Appending Text
Insert or append text to specific lines:
sed '3i\Inserted text' file.txt # Inserts "Inserted text" before line 3
sed '3a\Appended text' file.txt # Appends "Appended text" after line 3
Additional Help
You can explore more by using the commands:
sed --help
# or
man sed
Resources
15 Useful ‘sed’ Command Tips and Tricks for Linux SysAdmins
Thank you!
Thank you for your time and for reading this!