Courseiva

LPIC-1 Shells, Scripting and Data Management Practice Question

Which TWO of the following commands can be used to replace text patterns in a file and output the result?

⚠ Common exam trap

Test-takers frequently confuse grep's pattern-matching capability with text replacement, assuming it can modify content, when in fact grep only filters lines and does not alter or output transformed text.

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

✓

awk

Option A, awk, is correct because awk is a full text-processing language that supports substitution via the gsub() function (e.g., awk '{gsub(/old/,"new"); print}' file), which replaces matching patterns and prints the modified result to standard output. Option C, sed, is correct because sed is the classic stream editor whose s/// substitution command (e.g., sed 's/old/new/g' file) replaces text patterns and writes the transformed content to stdout without altering the original file. Option B, grep, is not correct because grep only searches for and prints lines matching a pattern; it does not replace text. Option D, tr, is not correct because tr translates or deletes individual characters rather than replacing multi-character text patterns. Option E, cut, is not correct because cut extracts selected fields or columns from each line and performs no substitution.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    awk

    Why this is correct

    awk processes input line by line, and its gsub() function substitutes every match of a pattern within a field or record, writing the modified text to standard output. This satisfies the requirement to replace text patterns and output the result without altering the source file.

  • ✗

    grep

    Why it's wrong here

    grep matches and prints lines containing a pattern; it cannot substitute matched text with replacement content, so it produces no modified output. It is tempting because it is the standard tool for pattern searching, and would be correct if the task were merely locating matching lines rather than replacing them.

  • ✓

    sed

    Why this is correct

    sed is a stream editor that reads input, applies substitution commands such as s/pattern/replacement/ to each line, and writes the transformed text to standard output. This directly satisfies the requirement to replace text patterns in a file and output the result.

  • ✗

    tr

    Why it's wrong here

    tr translates or deletes individual characters from standard input; it cannot match multi-character patterns or edit a file in place. It tempts because character substitution resembles replacement, but it is correct only for tasks such as deleting or squeezing single characters in a stream.

  • ✗

    cut

    Why it's wrong here

    cut extracts selected fields or character ranges by delimiter or position; it never substitutes matched text. It tempts because it processes file content and writes to stdout, but it is correct only for column extraction, not for replacing patterns with new strings.

About these practice questions

One of 402 original LPIC-1 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.