Which of the following commands will reduce all consecutive spaces down to a single space?
正解: E
Explanation
The command that will reduce all consecutive spaces down to a single space is tr -s ' ' < a.txt > b.txt. This command uses the following options and syntax:
* -s: Squeezes repeated characters listed in the first set with single occurrence.
* ' ': Specifies a space character as the first set.
* < a.txt: Redirects the input from a file named a.txt.
* b.txt: Redirects the output to a file named b.txt.
The output of this command will be a new file called b.txt that contains the same text as a.txt, except that any sequence of multiple spaces will be replaced by a single space. For example, if the file a.txt contains the following text:
This is a text file with multiple spaces.
The file b.txt will contain the following text:
This is a text file with multiple spaces.
The other commands are incorrect for the following reasons:
* A. tr '\s' ' ' < a.txt > b.txt: This command will replace every whitespace character (\s) with a space character, which will not reduce the number of spaces, but rather convert tabs and newlines into spaces.
* B. tr -c ' ' < a.txt > b.txt: This command will complement the first set, meaning that it will apply the operation to all characters that are not spaces. This will not affect the spaces at all, but rather squeeze all other characters.
* C. tr -d ' ' < a.txt > b.txt: This command will delete all spaces from the input, which will not reduce them to a single space, but rather remove them completely.
* D. tr -r ' ' '\n' < a.txt > b.txt: このコマンドは、すべてのスペースを改行に置き換えます。スペースは削減されず、単語ごとに新しい行が作成されます。
参考文献:
* [LPI 試験 101 詳細な目標]、トピック 103: GNU および Unix コマンド、目標 103.2: フィルターを使用してテキスト ストリームを処理する、重み付け: 3、主要な知識領域: tr を使用して文字を変換したり、文字を圧縮したり削除したりします。
* LinuxのTrコマンドと例、トピック: 繰り返し文字列を圧縮する方法
-s オプション。