Linux Text Processing Cheat Sheet/Trim trailing whitespace

Remove spaces and tabs from line endings.

Section: Formatting and Cleanup

Trim trailing whitespace

bash
bash
sed 's/[[:space:]]*$//' file.txt
Explanation

Useful before committing scripts, YAML, or source files.

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
Squeeze repeated spaces
Collapse multiple spaces into one.
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
Print a specific line range
Extract lines 100 through 120.
OpenIn sheetbash1 tag match
Pipe file content into grep
Basic pipeline for filtering text from a file.