tr -s: Squeeze Repeated Characters
tr -s squeezes each run of repeated characters in the set down to a single character.
Tool output often has runs of spaces that misalign columns. tr -s flattens them so cut and read behave.
What it does
tr -s SET replaces each sequence of repeated characters from SET with a single instance. tr -s ' ' turns multiple spaces into one, which is the classic prep step before cut -d' ', since cut treats every space as a separator.
Common usage
ps aux | tr -s ' ' | cut -d' ' -f2 # squeeze, then take PID
echo "a,,,b" | tr -s ',' # a,b
df | tr -s ' ' | cut -d' ' -f5 # use% column
tr -s '\n' < file.txt # collapse blank linesOptions
| Flag | What it does |
|---|---|
| -s | Squeeze runs of characters in SET to one |
| SET | Which characters to squeeze, e.g. ' ' or '\n' |
| -s with SET1 SET2 | Translate first, then squeeze SET2 |
| -c | Complement the set before squeezing |
In CI
Put tr -s ' ' before cut when parsing column output from ps, df, or ls -l, which pad columns with variable spacing. Without it, cut -d' ' produces empty fields and your -f index is wrong.
Common errors in CI
tr -s ' ' does not trim a leading space, so the first cut field may still be empty; many add sed 's/^ //' or use awk instead. Squeezing newlines collapses blank lines but keeps a single newline, which is usually intended. Locale can affect what counts as whitespace under classes; use LC_ALL=C for ASCII spaces.