Reduce Consecutive Spaces to Single Space | LFCS Linux Exam | Linux Foundation Certified System Administrator

Reduce Consecutive Spaces to Single Space

Question

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

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

E

The correct answer is E. tr -s ' ' < a.txt > b.txt.

The tr command is used to translate, delete or squeeze characters. Here, we want to squeeze consecutive spaces down to a single space.

Let's go through the options:

A. tr '\s' ' ' < a.txt > b.txt: This command replaces all whitespace characters with a single space. It will replace spaces, tabs, and newlines with a space. So, it won't reduce consecutive spaces down to a single space.

B. tr -c ' ' < a.txt > b.txt: This command replaces all characters except spaces with a space. It won't reduce consecutive spaces down to a single space.

C. tr -d ' ' < a.txt > b.txt: This command deletes all spaces. It won't reduce consecutive spaces down to a single space.

D. tr -r ' ' '\n' < a.txt > b.txt: This command replaces all spaces with newlines. It won't reduce consecutive spaces down to a single space.

E. tr -s ' ' < a.txt > b.txt: This command squeezes consecutive spaces down to a single space. It will replace all consecutive spaces with a single space, reducing the number of spaces.

So, the correct command to reduce all consecutive spaces down to a single space is tr -s ' ' < a.txt > b.txt.