Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

tr Command Guide for Translating and Deleting Characters

This guide helps you understand and use the tr command, the standard way to translate, squeeze, or delete single characters in a stream.


What is tr, and Why Use It?

The tr command (“translate”) reads a stream and performs character-by-character transformations: change case, replace one character with another, delete characters, or squeeze runs of repeated characters into one. It can’t work on whole words or patterns — it works on characters, which makes it tiny, fast, and perfect for pipeline glue.

Why Learn tr?

  • Case conversion: tr '[:lower:]' '[:upper:]' uppercases a stream in one step.
  • Pipeline glue: tr ' ' '\n' turns words into lines so sort and uniq can work on them.
  • Cleaning data: Squeeze repeated spaces or delete stray punctuation before analysis.
  • It’s everywhere: POSIX-standard, in every shell script for forty years.

tr transforms characters, never words or patterns. For those, reach for sed.


1. Change Case

echo "hello world" | tr '[:lower:]' '[:upper:]'
  • [:lower:] and [:upper:] are character classes: every lowercase letter becomes uppercase.
cat file.txt | tr '[:upper:]' '[:lower:]'
  • The reverse: everything to lowercase. Useful before comparing or deduplicating text with inconsistent capitalization.

2. Replace Characters

echo "2026/12/04" | tr '/' '-'
  • Every / becomes -. tr maps characters one-to-one: the first set is the source, the second set the target.
tr 'a-z' 'A-Z' < input.txt > output.txt
  • tr reads stdin when no file is given, and writes to stdout — it’s a natural pipeline member, not a file editor.

Note the one-to-one rule: tr 'abc' 'X' maps a→X, b→X, c→X (the second set is reused). But tr 'ab' 'XYZ' maps a→X, b→Y and leaves Z unused — sets of different lengths still work by reuse, so keep them aligned when order matters.


3. Delete Characters

echo "a1b2c3" | tr -d '0-9'
  • -d deletes every digit, leaving abc.
echo "hello   world" | tr -s ' '
  • -s squeezes runs of the given character into a single one: three spaces become one.
tr -d '\r' < windows-file.txt
  • The classic fix: strip carriage returns from files created on Windows. \r is the invisible culprit behind ^M artifacts.

4. The Classic Pipeline Glue

tr’s most famous job is turning one kind of whitespace into another so other tools can work:

cat words.txt | tr ' ' '\n' | sort | uniq -c | sort -rn | head
  • tr ' ' '\n' puts one word per line, which is exactly the shape sort and uniq expect. This is the word-frequency pipeline explained in the section README.

Split on tabs instead, for column-like input:

tr '\t' '\n' < data.tsv
  • Every tab becomes a newline, so each field lands on its own line.

5. Complement and Keep Sets

echo "keep-this-and-that" | tr -c 'a-z' '#'
  • -c complements the set: everything that is not a lowercase letter gets replaced. Here, the hyphens become #.
echo "user:name" | tr -d -c '[:alnum:]\n'
  • Keep only alphanumerics and newlines — delete everything else. Handy for sanitizing strings before using them in filenames or identifiers.

6. tr Inside Scripts

tr is a favorite in shell scripts because it never touches the filesystem unless you redirect:

name=$(echo "$INPUT" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
  • That builds a slug: lowercase, spaces replaced… wait, spaces were turned into newlines by the first class? No — the second tr ' ' '-' runs on what’s left. Order matters: convert case first, then replace spaces with hyphens, and skip the newline trap by keeping everything on one line.

A cleaner slug:

slug=$(echo "$INPUT" | tr '[:upper:]' '[:lower:]' | tr -s ' ' '-' | tr -d '[:punct:]')
  • Lowercase, collapse multiple spaces, turn spaces into hyphens, delete punctuation.

Common Pitfalls

  • tr can’t translate multi-character strings. tr 'abc' 'xyz' works on single characters only. For strings, use sed.
  • Different-length sets are confusing. tr maps positionally and reuses the last target character. Keep sets the same length when the mapping must be exact.
  • tr -d '\r' needs the escape interpreted. Use tr -d '\r' (with quotes) — some shells pass a literal backslash-r otherwise.
  • tr operates on every line independently. It has no memory across lines, so it can’t “replace the second occurrence” or work contextually.
  • Character classes need brackets and colons: [:lower:], not lower or :lower:.

PageRelationship
sedReplaces whole strings and patterns, not just characters.
sortUsually runs right after tr in word-count pipelines.
uniqCounts the lines tr produced.
cutExtracts fields where tr would reshape delimiters.

tr Cheatsheet

CommandWhat It Does
tr 'a-z' 'A-Z'Lowercase to uppercase.
tr '[:upper:]' '[:lower:]'Uppercase to lowercase.
tr 'x' 'y'Replace every x with y.
tr -d 'x'Delete every x.
tr -s ' 'Squeeze repeated spaces into one.
tr -c 'a-z' '#'Replace everything that isn’t a letter.
tr ' ' '\n'One word per line.
tr -d '\r'Strip Windows carriage returns.