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.
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)- A4% (1)
- C4% (1)
- E92% (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.
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.
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.
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.
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.
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
Community Discussion
No community discussion yet for this question.