Problem Statement
What is the primary purpose of the sed command?
Explanation
Sed (Stream EDitor) is a powerful stream editor for filtering and transforming text. It reads input line by line, applies specified operations, and outputs the result. Sed is commonly used for search-and-replace operations, text manipulation, and automated editing. Unlike interactive editors, sed processes text programmatically, making it perfect for scripts and automation.
The most common use is substitution: sed 's/old/new/' filename replaces the first occurrence of 'old' with 'new' on each line. Add 'g' flag for global replacement: sed 's/old/new/g' replaces all occurrences. Use -i flag for in-place editing: sed -i 's/old/new/g' filename modifies the file directly.
Sed can delete lines (d command), insert lines (i command), append lines (a command), and perform complex transformations using regular expressions. It's invaluable for configuration file updates, log processing, and batch text transformations. Mastering sed significantly improves productivity in DevOps and system administration roles.
