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.
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
sedto 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.
-
sedsubstitution format:s/old/new/applies per-line. Thegflag 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
webserver.conf.
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.
protcol with
protocol in-place across the file.
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.
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
You are not in the right directory or the filename is wrong. Confirm with
ls and re-run cat webserver.conf.
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.
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.
# 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
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/: Substituteoldwithnewon each line (default is first match per line).g: Replace all matches on each line (not just the first).<file>: Target file to edit.