sed


The sed command, short for stream editor, is a powerful utility on RHEL and other UNIX-like systems. It excels at performing text transformations on an input stream or file, enabling you to filter, substitute, delete, and insert text based on patterns and regular expressions.


Example 1: Basic Substitution on a Line

# This command reads the 'employee.txt' file and replaces the first
# occurrence of the word "manager" with "director" on each line.
sed 's/manager/director/' employee.txt

Explanation

The s command in sed signals a substitution. It finds the first instance of the pattern "manager" in every line of employee.txt and replaces it with "director", printing the result to standard output.


Example 2: Global Substitution on a Line

# Replaces every occurrence of "dev" with "development" within the employee.txt file.
sed 's/dev/development/g' employee.txt

Explanation

By adding the g flag at the end of the substitution command, sed performs a global replacement. This changes all occurrences of "dev" on each line, not just the first one.


Example 3: In-place File Editing

# This command directly modifies the employee.txt file, replacing "test" with "qa".
# The -i option edits the file in-place.
sed -i 's/test/qa/' employee.txt

# For macOS or other BSD-based systems, a backup extension is required.
# sed -i '.bak' 's/test/qa/' employee.txt

Explanation

The -i option instructs sed to modify the file directly, a common task for script-based file updates in RHEL. In other UNIX versions like macOS, this flag behaves differently, requiring an extension argument to create a backup file (.bak in the example).


Example 4: Deleting Lines by Pattern

# Deletes any line containing the word "obsolete" from the output.
sed '/obsolete/d' system.log

Explanation

Here, any line matching the pattern obsolete is targeted. The d command then deletes the entire line from the output stream, which is useful for cleaning up log files or configuration data.


Example 5: Printing Only Matching Lines

# Suppresses default output and prints only lines containing "ERROR".
# The -n option prevents printing, while 'p' prints the matched line.
sed -n '/ERROR/p' application.log

Explanation

This command emulates the basic functionality of grep. The -n option disables automatic printing of every line, and the p command explicitly prints only those lines that match the specified pattern "ERROR".


Example 6: Using an Alternative Delimiter

# Replaces a file path using a pipe '|' as the delimiter
# because the pattern contains slashes.
sed 's|/var/www/html|/srv/www|' httpd.conf

Explanation

When your search pattern or replacement string contains a forward slash /, using a different delimiter like | or # improves readability. This avoids the need to escape the slashes within the pattern.


Example 7: Applying Multiple Expressions

# Executes two separate substitution commands in a single pass.
# First, changes "admin" to "sysadmin", then "user" to "guest".
sed -e 's/admin/sysadmin/' -e 's/user/guest/' user_list.txt

Explanation

The -e option allows you to stack multiple expressions in a single sed command. This is more efficient than piping the output of one sed command into another, as the file is read only once.


Example 8: Inserting Text Before a Match

# Inserts a new line "### CRITICAL CONFIGURATION ###" before the line
# containing the pattern "Listen 80".
sed '/Listen 80/i ### CRITICAL CONFIGURATION ###' httpd.conf

Explanation

The i command instructs sed to insert the provided text on a new line immediately before the line that matches the pattern. This is a practical way to add comments or configuration directives programmatically.