Squeeze Repeats
One option that I have seldom used in tr
command is -s
or --squeeze-repeats
:
replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character - TR(1)
The best way to explain it is to demonstrate by example:
|
|
Apply the -s
option:
|
|
The number of repeated character 1
has been squeezed into a single occurrence.
Therefore, the tr command has the following three operations:
- Translate (default, no options)
- Delete (
-d
) - Squeeze (
-s
)
To drop repeated characters, the second set is not necessary. This is very useful to remove extra spaces:
|
|
In Vim, to squeeze the extra blank in a line:
|
|
Additional examples from GNU Coreutils Manual.
Delete blank lines:
|
|
Put all words on lines by themselves:
|
|
Quick summary for above mentioned use cases:
- Remove extra spaces
- Delete blank lines
- Put all words on lines by themselves
Notes:
|
|