What if you could change hundreds of words in seconds without touching a text editor?
Why sed for substitution in scripts in Bash Scripting? - 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 editing each word manually would take forever.
Manually searching and replacing words in large files is slow and tiring. You might miss some words or make mistakes. It's easy to get frustrated and waste a lot of time.
The sed command lets you quickly and automatically replace words or phrases in files or streams. With just one simple command, you can change all occurrences instantly, saving time and avoiding errors.
Open file, find 'apple', replace with 'orange', save, repeat for every line
sed 's/apple/orange/g' input.txt > output.txtYou can automate text changes in files or outputs instantly, making your scripts powerful and efficient.
Updating configuration files to replace old server names with new ones across hundreds of files without opening each file manually.
Manual text edits are slow and error-prone.
sed automates substitution with a simple command.
This saves time and reduces mistakes 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/old/new/which replaces only the first match per line.Step 2: Apply to the file content
Each line in file.txt will have its first 'cat' replaced by 'dog', other occurrences remain unchanged.Final Answer:
Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option CQuick Check:
sed s/old/new/ replaces first match per line [OK]
- Thinking it replaces all matches without 'g'
- Confusing substitution with deletion
- Assuming it prints only matching lines
Solution
Step 1: Identify the global flag usage
To replace all occurrences on each line, the 'g' flag is needed after the substitution pattern.Step 2: Check each option
sed 's/apple/orange/g' file.txt usess/apple/orange/gwhich correctly replaces all 'apple' with 'orange'. sed 's/apple/orange/' file.txt replaces only first occurrence. sed 'replace apple with orange' file.txt is invalid syntax. sed 's/apple/orange//g' file.txt has extra slashes causing syntax error.Final Answer:
sed 's/apple/orange/g' file.txt -> Option DQuick Check:
Use 'g' flag for all matches [OK]
- Omitting the 'g' flag to replace all matches
- Adding extra slashes causing syntax errors
- Using incorrect command words like 'replace'
apple apple apple?
sed 's/apple/orange/' file.txt
Solution
Step 1: Understand sed substitution without 'g'
Without the 'g' flag, sed replaces only the first occurrence of 'apple' per line.Step 2: Apply substitution to the input line
The first 'apple' becomes 'orange', the rest remain unchanged, resulting in 'orange apple apple'.Final Answer:
orange apple apple -> Option BQuick Check:
First match replaced only without 'g' [OK]
- Assuming all occurrences change without 'g'
- Confusing order of words after substitution
- Thinking sed changes input file by default
sed 's/foo/bar/' file.txt. But only the first 'foo' per line is replaced. What is the fix?Solution
Step 1: Identify missing global flag
The command lacks the 'g' flag, so only first occurrence per line is replaced.Step 2: Add the 'g' flag to fix
Adding 'g' after the substitution pattern makes sed replace all occurrences per line.Final Answer:
Add the 'g' flag: sed 's/foo/bar/g' file.txt -> Option AQuick Check:
Global flag 'g' replaces all matches [OK]
- Thinking quotes affect substitution behavior
- Adding spaces inside substitution pattern
- Confusing in-place edit with substitution flags
Solution
Step 1: Understand conditional substitution in sed
To apply substitution only on lines matching a pattern, use/pattern/ s/old/new/flagssyntax.Step 2: Analyze options
sed '/log/ s/error/warning/g' file.txt correctly applies substitution only on lines containing 'log'. Options A and C have invalid syntax. sed 's/error/warning/g' file.txt | grep log replaces everywhere then filters lines, which does not limit substitution.Final Answer:
sed '/log/ s/error/warning/g' file.txt -> Option AQuick Check:
Use address before s/// to limit lines [OK]
- Placing substitution after pattern incorrectly
- Trying to use if statements inside sed
- Filtering lines after substitution instead of limiting it
Ready to prove it?
Pass the test and this pattern moves unknown
