Linux Text Processing Cheat Sheet/Squeeze repeated spaces

Collapse multiple spaces into one.

Section: Formatting and Cleanup

Squeeze repeated spaces

bash
bash
tr -s ' ' < input.txt
Explanation

Good for cleaning space-delimited text before further parsing.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Formatting and Cleanup
Trim leading whitespace
Remove spaces and tabs from the start of each line.
OpenIn sheetbashsame section
Trim trailing whitespace
Remove spaces and tabs from line endings.
OpenIn sheetbashsame section
Wrap text to 72 columns
Reflow long text paragraphs.
OpenIn sheetbashsame section
Force hard wraps
Break long lines at a fixed width.
OpenIn sheetbashsame section
Pipe file content into grep
Basic pipeline for filtering text from a file.
Show first 20 lines
Preview the top of a file.