Creating a comprehensive tutorial on the sed
command in Linux, covering a wide range of use cases, will be quite extensive, but I’ll do my best to provide a detailed guide.
Introduction to sed:
sed, short for stream editor, is a powerful command-line tool used for parsing and transforming text. It’s particularly useful for performing operations like search, replace, insert, and delete on text files without modifying the original file.
Basic Syntax:
The basic syntax of sed command is:
sed OPTIONS 'COMMAND' INPUTFILE
- OPTIONS: Optional parameters to modify sed behavior.
- ‘COMMAND’: The action to perform on the text.
- INPUTFILE: The file(s) to process. If not provided, sed reads from standard input.
Common Options:
- -e: Allows specifying multiple commands.
- -i: Edit files in place (i.e., modify the original file).
- -n: Suppress automatic printing of pattern space.
- -r or -E: Use extended regular expressions (support for extended syntax).
Common Commands:
- Search and Replace:
- This command replaces all occurrences of ‘old_text’ with ‘new_text’ in the specified file.
-
sed 's/old_text/new_text/g' filename
- Delete Lines:
- Deletes all lines containing the specified pattern.
-
sed '/pattern/d' filename
- Insert and Append:
- Inserts ‘new_line’ before the line containing ‘pattern’.
-
sed '/pattern/i\new_line' filename
-
- Appends ‘new_line’ after the line containing ‘pattern’.
-
sed '/pattern/a\new_line' filename
-
- Inserts ‘new_line’ before the line containing ‘pattern’.
- Print Specific Lines:
- Prints lines from n1 to n2.
-
sed -n 'n1,n2p' filename
-
- Prints lines from n1 to n2.
- Perform Multiple Operations :
- Executes multiple commands sequentially.
-
sed -e 'command1' -e 'command2' filename
-
- Executes multiple commands sequentially.
- Transform Delimiters:
- Using different delimiters, like ‘|’, instead of ‘/’.
-
sed 's|old_text|new_text|g' filename
-
- Using different delimiters, like ‘|’, instead of ‘/’.
Advanced Usage:
- Regular Expressions:
- sed supports powerful regular expressions for pattern matching and manipulation.
- For example:
-
sed 's/[0-9]//g' filename: Removes all digits.
-
sed '/^#/d' filename: Deletes lines starting with '#'.
-
- Backreferences:
- You can reference captured groups in the replacement text. For example:
-
sed 's/\(pattern1\)\(pattern2\)/\2\1/g' filename
-
- You can reference captured groups in the replacement text. For example:
- Conditional Operations:
- You can perform conditional operations using the sed branching feature.
-
sed '/pattern/{COMMAND}' filename
- Hold and Pattern Space:
- sed maintains two spaces: pattern space (default) and hold space. They can be manipulated using h, H, g, and G commands.
Examples:
- Replace all occurrences of ‘apple’ with ‘banana’ in ‘file.txt‘ and save the changes:
-
sed -i 's/apple/banana/g' file.txt
-
- Delete all lines containing ‘xyz’ from ‘file.txt’:
-
sed -i '/xyz/d' file.txt
-
- Insert ‘hello’ before the first line of ‘file.txt’:
-
sed -i '1 i\hello' file.txt
-
Conclusion
This tutorial provides a solid foundation for using sed in Linux, covering basic to advanced operations. Experimenting with these commands and exploring more features will further enhance your proficiency with sed.
We will be sharing a detailed sed command tutorial on Youtube.