tr — Character Translation
Character-level translation, deletion, and squeeze — the preprocessing step before sort, awk, or grep.
Character Translation
Translate lowercase to uppercase
echo "hello world" | tr 'a-z' 'A-Z'
Translate uppercase to lowercase
echo "HELLO WORLD" | tr 'A-Z' 'a-z'
Using POSIX character classes for locale-safe case conversion
echo "hello world" | tr '[:lower:]' '[:upper:]'
Replace spaces with underscores — filename sanitization
echo "my file name.txt" | tr ' ' '_'
Replace colons with newlines — break PATH into lines
echo "$PATH" | tr ':' '\n'
ROT13 — simple letter rotation cipher
echo "hello" | tr 'a-zA-Z' 'n-za-mN-ZA-M'
Delete Characters — -d
Delete all digits
echo "abc123def456" | tr -d '[:digit:]'
Delete all whitespace
echo " h e l l o " | tr -d '[:space:]'
Strip carriage returns — fix Windows line endings
tr -d '\r' < windows.txt > unix.txt
Remove all non-printable characters
tr -d '[:cntrl:]' < binary_mixed.txt
Delete specific characters — strip quotes
echo '"quoted"' | tr -d '"'
Squeeze Repeats — -s
Squeeze repeated spaces into one — normalize whitespace
echo "too many spaces" | tr -s ' '
Squeeze repeated newlines — collapse blank lines
tr -s '\n' < multi_blank.txt
Squeeze tabs to single tab
tr -s '\t' < messy.tsv
Complement — -c
Delete everything that is NOT a letter or newline — keep only alphabetic chars
echo "abc123!@#def" | tr -cd '[:alpha:]\n'
Keep only digits and newlines
echo "phone: (555) 123-4567" | tr -cd '[:digit:]\n'
Replace all non-alphanumeric chars with dashes — slug generation
echo "Hello, World! (2026)" | tr -cs '[:alnum:]' '-' | tr 'A-Z' 'a-z'
Delete everything except printable characters — sanitize input
tr -cd '[:print:]\n' < suspect.txt
Newline Handling
Join all lines into one — replace newlines with spaces
tr '\n' ' ' < multiline.txt && echo
Join lines with commas — then trim trailing comma
tr '\n' ',' < list.txt | sed 's/,$/\n/'
Split on commas to newlines
echo "a,b,c,d,e" | tr ',' '\n'
Replace null bytes with newlines — process find -print0 output without xargs
find /var/log -name "*.log" -print0 | tr '\0' '\n'
Character Classes Reference
Available POSIX character classes
# [:upper:] Uppercase letters
# [:lower:] Lowercase letters
# [:alpha:] All letters
# [:digit:] Digits 0-9
# [:alnum:] Letters and digits
# [:space:] Whitespace (space, tab, newline, etc.)
# [:blank:] Space and tab only
# [:print:] Printable characters (includes space)
# [:graph:] Printable characters (excludes space)
# [:cntrl:] Control characters
# [:punct:] Punctuation characters
# [:xdigit:] Hexadecimal digits (0-9, a-f, A-F)
Practical Pipelines
Normalize a file — lowercase, squeeze spaces, strip non-printable
tr 'A-Z' 'a-z' < input.txt | tr -s '[:space:]' | tr -cd '[:print:]\n'
Word frequency — split words, count
tr -cs '[:alpha:]' '\n' < document.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -rn | head -20
Generate random password characters — use /dev/urandom with tr filter
tr -dc '[:alnum:]!@#$%' < /dev/urandom | head -c 24 && echo
Convert tabs to spaces — simple formatting
tr '\t' ' ' < tabbed.txt