What this template solves
This template is useful when text contains irregular spacing and you want to compress repeated spaces without changing line breaks.
Sample input
alpha beta gamma
one twoPrompt to start from
Collapse multiple spaces into a single space
Expected command
sed 's/ */ /g'Tags
Best for
- Normalize copied text with uneven spacing.
- Clean reports or tabular exports before sharing.
- Make noisy plain text easier to diff or parse.
How to use this template
- 1Check whether only repeated spaces should collapse, not tabs or line breaks.
- 2Copy the sed command that compresses multiple spaces.
- 3Preview on representative text before applying the file-edit command.
Template FAQ
How do I collapse multiple spaces into one with sed?
A common pattern is `sed 's/ */ /g'`, which compresses repeated spaces into a single space while leaving line breaks intact. It is helpful for copied text and uneven plain text formatting.
What is the difference between collapse-multiple-spaces and normalize-whitespace?
Collapse-multiple-spaces focuses only on repeated spaces, while normalize-whitespace is broader and can also target tabs or other whitespace classes depending on the command pattern.
Related templates
Explore nearby sed workflows that target similar replace, cleanup, filtering, or rewrite tasks.
Normalize whitespace
CleanupCollapse repeated spaces and tabs into one clean separator.
sed 's/[[:space:]]\+/ /g'Remove duplicate blank lines
CleanupCollapse repeated blank lines so only one empty row remains.
sed '/^$/N;/^\n$/D'Remove trailing spaces
CleanupDelete trailing spaces and tabs at the end of each line.
sed 's/[[:space:]]\+$//'