Loading...

Lab 3: Basic sed Substitution

Correct a recurring configuration typo using a safe, repeatable sed substitution pattern. Preview the file, apply an in-place global replacement, then verify the result.

core troubleshooting services

Scenario

You’ve been handed a config file called webserver.conf with a typo. All instances of protcol need to be corrected to protocol. You are expected to use sed to fix it across the entire file in-place.

Operator context

This is a common system administration task: applying a safe text change across configuration files without opening an editor, then verifying the results immediately.

Objective

  • Preview a configuration file using cat.
  • Use sed to perform an in-place substitution across the file.
  • Verify the corrected contents after the change.

Concepts

  • A safe text change starts with inspection: confirm the current state before you write anything.
  • sed substitution format: s/old/new/ applies per-line. The g flag controls “all matches on the line.”
  • In-place edits are powerful and risky; you verify immediately after the change.
  • Repeatability matters: a single command that can be run consistently beats a manual edit that cannot be audited.

Walkthrough

Step 1: Preview the contents of webserver.conf.
Command
cat webserver.conf

Confirm the typo exists before changing anything. This also lets you verify you are working in the right file.

server_name localhost;
protcol http;
listen 80;
protcol must be set correctly.
Step 2: Replace protcol with protocol in-place across the file.
Command
sed -i 's/protcol/protocol/g' webserver.conf

This uses sed substitution syntax s/old/new/ and applies it globally on each line with g. The -i flag updates the file in-place.

Step 3: Verify that the typo has been corrected.
Command
cat webserver.conf

Always re-check after an in-place edit to confirm you changed what you intended and nothing else.

server_name localhost;
protocol http;
listen 80;
protocol must be set correctly.

Common breakpoints

sed: can't read webserver.conf

You are not in the right directory or the filename is wrong. Confirm with ls and re-run cat webserver.conf.

No change after running sed

The match text may differ (case, whitespace, or a different typo). Use grep -n protcol webserver.conf to confirm what is actually in the file.

Permission denied during in-place edit

The file may be owned by root or not writable. Check permissions with ls -l and re-run the edit using sudo if appropriate.

Cleanup checklist

This lab modifies a single file. Cleanup is returning the file to its starting state if needed.

Optional
# If you want to reset the file for another repetition, reintroduce the typo:
sed -i 's/protocol/protcol/g' webserver.conf

# Verify
cat webserver.conf
Success signal

You can inspect, apply a targeted change, and verify results without relying on an interactive editor.

Reference

  • cat: Prints file contents to standard output.
  • cat <file>: Prints a file to standard output.
    • <file>: Path to the file to display.
  • sed: Stream editor for transforming text.
  • sed -i 's/old/new/g' <file>: Performs a substitution and writes changes back to the file.
    • -i: Edit the file in place (modify the file directly).
    • s/old/new/: Substitute old with new on each line (default is first match per line).
    • g: Replace all matches on each line (not just the first).
    • <file>: Target file to edit.