LFCS · Question #812
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 command with the -s (squeeze) option is used to replace sequences of identical characters with a single occurrence of that character.
Question
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
(28 responses)- A4% (1)
- D4% (1)
- E93% (26)
Why each option
The `tr` command with the `-s` (squeeze) option is used to replace sequences of identical characters with a single occurrence of that character.
The `tr` command with `\s` might not be universally interpreted as a whitespace character, and it does not perform squeezing of consecutive characters.
The `tr -c ' '` command complements the set of characters, meaning it operates on characters *not* in the specified set, which is not for reducing consecutive spaces.
The `tr -d ' '` command deletes all occurrences of the specified character, which would remove all spaces rather than reducing consecutive ones.
The `-r` option is not a standard or recognized option for the `tr` command, making this command syntactically incorrect for the intended purpose.
The `tr -s ' '` command uses the `-s` or `--squeeze-repeats` option to replace each sequence of two or more identical characters specified in SET1 with a single instance of that character. Therefore, it will reduce all consecutive spaces down to a single space.
Concept tested: `tr` command character squeezing
Source: https://man7.org/linux/man-pages/man1/tr.1.html
Topics
Community Discussion
No community discussion yet for this question.