nerdexam
CompTIA

LX0-103 · Question #212

Which of the following commands will reduce all consecutive spaces down to a single space?

The correct answer is E. tr -s ' ' < a.txt > b.txt. The tr -s option squeezes sequences of repeated consecutive characters into a single instance, making it the correct choice to collapse multiple spaces into one.

GNU and Unix Commands

Question

Which of the following commands will reduce all consecutive spaces down to a single space?

Options

  • Atr '\s' ' ' < a.txt > b.txt
  • Btr -c ' ' < a.txt > b.txt
  • Ctr -d ' ' < a.txt > b.txt
  • Dtr -r ' ' '\n' < a.txt > b.txt
  • Etr -s ' ' < a.txt > b.txt

How the community answered

(24 responses)
  • A
    4% (1)
  • C
    4% (1)
  • E
    92% (22)

Why each option

The tr -s option squeezes sequences of repeated consecutive characters into a single instance, making it the correct choice to collapse multiple spaces into one.

Atr '\s' ' ' < a.txt > b.txt

tr interprets its SET1 argument as a list of literal characters, not a regex, so '\s' matches a literal backslash and the letter s rather than whitespace - this does not squeeze spaces.

Btr -c ' ' < a.txt > b.txt

The -c flag complements SET1, so -c ' ' applies the operation to every character that is NOT a space - this would squeeze repeated non-space characters instead.

Ctr -d ' ' < a.txt > b.txt

The -d flag deletes every occurrence of the specified character entirely, so tr -d ' ' removes all spaces from the output rather than reducing consecutive ones to a single space.

Dtr -r ' ' '\n' < a.txt > b.txt

The -r flag is not defined in POSIX or GNU tr and causes an error on standard systems; reversing the operand order is done by swapping SET1 and SET2 arguments, not with a -r flag.

Etr -s ' ' < a.txt > b.txtCorrect

The -s (squeeze-repeats) flag instructs tr to replace each run of consecutive identical characters listed in SET1 with exactly one instance of that character. Running tr -s ' ' reads from a.txt and writes to b.txt, collapsing any sequence of two or more adjacent spaces into a single space while leaving all other characters untouched.

Concept tested: tr command squeeze-repeats option for whitespace

Source: https://man7.org/linux/man-pages/man1/tr.1.html

Topics

#tr#squeeze#whitespace#text processing

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice