Mastering the sed Command: In-Place Editing, Substitution, and Line Deletion
Which THREE statements are true about the sed command?
Quick Answer
The correct answer identifies that sed -n '3,5p' file.txt prints lines 3 to 5, as the -n flag suppresses automatic output and the p command explicitly prints only the specified range. This is true because sed operates as a stream editor, processing text line by line, and the combination of -n with the address range and print command gives you precise control over which lines appear in the output. On the LPIC-1 exam, this tests your understanding of sed command options and usage, particularly how flags like -i for in-place editing and substitution syntax like 's/foo/bar/g' modify files directly rather than just displaying changes. A common trap is confusing -n with -i: remember that -n silences output, while -i writes changes back to the file. For memory, think "n for no print unless told, i for in-file edit."
⚠ Common exam trap
A common mix-up: candidates assume sed always modifies files in place, forgetting that without `-i`, sed only outputs to stdout, and that sed defaults to basic regular expressions, not extended ones.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
sed -i 's/foo/bar/g' file.txt replaces all occurrences of foo with bar in the file.
The `-i` flag in sed enables in-place editing, directly modifying the file rather than just outputting changes to stdout. The substitution command `'s/foo/bar/g'` replaces all occurrences of `foo` with `bar` globally on each line, and with `-i`, the changes are written back to the file.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
sed 's/old/new/g' file.txt permanently changes the file.
Why it's wrong here
Without -i, sed outputs to stdout, does not change the file.
- ✓
sed -i 's/foo/bar/g' file.txt replaces all occurrences of foo with bar in the file.
Why this is correct
-i makes in-place changes.
- ✗
sed uses extended regular expressions by default.
Why it's wrong here
By default, sed uses basic regular expressions; -E enables extended.
- ✓
sed '/^#/d' file.txt deletes lines that start with #.
Why this is correct
The address /^#/ matches lines starting with #, d deletes them.
- ✓
sed -n '3,5p' file.txt prints lines 3 to 5 of file.txt.
Why this is correct
-n suppresses output, p prints specified lines.
Go deeper
Related to this question
About these practice questions
Courseiva writes every LPIC-1 question from scratch — 527 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on LPIC-1
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Which TWO commands can be used to count the number of lines in a file named 'data.txt'?
hard- ✓ A.wc -l data.txt
- ✓ B.awk 'END{print NR}' data.txt
- C.cat data.txt | wc -c
- D.grep -c '.*' data.txt
- E.sed -n '$=' data.txt
Why A: `wc -l` specifically counts the number of newline characters in the file, which corresponds to the number of lines. Option B is correct because `awk 'END{print NR}'` processes the file line by line, and the built-in variable `NR` holds the total number of records (lines) processed when the END block is executed, thus outputting the line count.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This LPIC-1 practice question is part of Courseiva's free LPI certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the LPIC-1 exam.