Introduction:
In the realm of Linux command-line tools, one utility stands out as a powerful text manipulation powerhouse: Sed. Short for “stream editor,” Sed is designed to modify and transform text streams with ease and efficiency. In this blog, we’ll explore the capabilities of Sed and demonstrate its usage through detailed explanations and practical examples.
Overview of Sed:
Sed operates on a line-by-line basis, reading input from a specified file or standard input (piped input), applying rules or commands to each line, and then printing the modified output. It uses a simple, yet expressive, syntax to define editing operations. The basic format of a Sed command is as follows:
sed [options] 'command' [input_file]
Commonly Used Sed Commands:
- Print Lines (p) Command: The ‘p’ command prints the selected lines, either explicitly defined or based on patterns. For example, to print all lines in a file, use the following command:
sed -n 'p' file.txt
- Substitute (s) Command: The ‘s’ command is used to substitute text patterns with desired replacements. It follows the syntax:
's/pattern/replacement/flags'
. For instance, to replace the word “apple” with “banana” in a file, use the following command:
sed 's/apple/banana/g' file.txt
- Delete (d) Command: The ‘d’ command deletes lines based on patterns or line numbers. To remove lines containing a specific pattern, use:
sed '/pattern/d' file.txt
- Append (a) and Insert (i) Commands: The ‘a’ command appends text after the selected lines, and the ‘i’ command inserts text before the selected lines. For example, to append a line after the occurrence of a pattern:
sed '/pattern/a New line to append' file.txt
- Transform (y) Command: The ‘y’ command performs character-wise substitution. It replaces each character from the source set with the corresponding character from the destination set. To convert lowercase letters to uppercase:
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' file.txt
Advanced Sed Commands:
- Address Ranges: Sed supports specifying address ranges to apply commands selectively. For example, to perform the substitution on lines 5 to 10 only:
sed '5,10s/pattern/replacement/g' file.txt
- Regular Expressions: Sed leverages the power of regular expressions for pattern matching. Regular expressions allow for complex pattern definitions. To match lines starting with “Hello” and ending with a digit:
sed -n '/^Hello.*[0-9]$/p' file.txt
- Writing to a File: Sed can write its output to a file instead of printing to the standard output. To overwrite the input file with the modified content:
sed -i 's/apple/banana/g' file.txt
- Multiple Commands: Multiple commands can be executed sequentially using the ‘-e’ option. For example, to substitute “apple” with “banana” and delete lines containing “orange”:
sed -e 's/apple/banana/g' -e '/orange/d' file.txt
Conclusion:
Sed is a versatile and powerful tool for text manipulation on Linux. With its rich set of commands, regular expressions, and address ranges, Sed enables you to perform complex transformations efficiently. By mastering the Sed command, you can streamline text processing tasks and automate editing operations.