What if you could fix hundreds of text mistakes with just one simple command?
Why sed substitution in Linux CLI? - Purpose & Use Cases
Imagine you have a long text file with hundreds of lines, and you need to change every instance of the word "apple" to "orange". Doing this by opening the file and manually editing each occurrence would take forever.
Manually searching and replacing words in large files is slow and tiring. You might miss some words or accidentally change the wrong ones. It's easy to make mistakes, and fixing them wastes even more time.
The sed substitution command lets you quickly and safely replace text patterns in files or streams. With a simple command, you can change all matching words instantly, without opening the file or risking errors.
Open file, find 'apple', replace with 'orange', save, repeat for each occurrence
sed 's/apple/orange/g' input.txt > output.txtYou can automate text changes across many files or large data instantly, saving hours of manual work.
Updating a configuration file where a server name changed from "server1" to "server2" across dozens of lines without opening the file manually.
sed substitution automates text replacement in files or streams.
It is fast, reliable, and reduces human errors.
Perfect for batch editing or quick fixes in scripts.
Practice
sed command sed 's/cat/dog/' file.txt do?Solution
Step 1: Understand the sed substitution syntax
The commandsed 's/cat/dog/' file.txtusess/pattern/replacement/which replaces the first match per line.Step 2: Apply the command to the file
It replaces only the first 'cat' found on each line with 'dog', not all occurrences or lines.Final Answer:
Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option AQuick Check:
sed s/pattern/replacement/ replaces first match per line [OK]
- Thinking all matches are replaced without 'g'
- Confusing substitution with deletion
- Assuming it prints only matching lines
sed?Solution
Step 1: Identify the flag for global replacement
The 'g' flag after the substitution pattern tells sed to replace all occurrences on each line.Step 2: Check syntax correctness
sed 's/apple/orange/g' file.txt usessed 's/apple/orange/g'which is the correct syntax without extra characters.Final Answer:
sed 's/apple/orange/g' file.txt -> Option BQuick Check:
'g' flag means replace all matches per line [OK]
- Omitting the 'g' flag for all replacements
- Adding extra semicolons inside quotes
- Using 'gp' which is invalid here
input.txt contains:apple apple banana banana apple apple
sed 's/apple/orange/' input.txt?Solution
Step 1: Understand sed substitution without 'g'
Without 'g', only the first 'apple' on each line is replaced by 'orange'.Step 2: Apply substitution line by line
Line 1: 'apple apple banana' becomes 'orange apple banana'. Line 2: 'banana apple apple' becomes 'banana orange apple'.Final Answer:
orange apple banana banana orange apple -> Option CQuick Check:
First match per line replaced only [OK]
- Replacing all 'apple' without 'g'
- Replacing none due to misunderstanding
- Confusing line order or content
sed 's/hello/world/g file.txt'Solution
Step 1: Check the quotes in the command
The command has an opening single quote before s/hello/world/g but no closing quote after 'g'.Step 2: Confirm correct placement of quotes and arguments
The file name should be outside the quotes. The missing closing quote causes syntax error.Final Answer:
Missing closing quote after 'g' -> Option DQuick Check:
Quotes must enclose entire sed expression [OK]
- Placing file name inside quotes
- Forgetting closing quote
- Confusing delimiter with quotes
data.txt. Which command achieves this?Solution
Step 1: Understand address restriction in sed
Using/baz/ s/foo/bar/gapplies substitution only on lines matching 'baz'.Step 2: Verify command correctness
sed '/baz/ s/foo/bar/g' data.txt correctly restricts substitution to lines with 'baz' and replaces all 'foo' with 'bar'.Final Answer:
sed '/baz/ s/foo/bar/g' data.txt -> Option AQuick Check:
Address before command limits lines affected [OK]
- Placing substitution after file name
- Using grep after sed instead of address
- Mixing patterns in wrong order
Ready to prove it?
Pass the test and this pattern moves unknown
