$_ CLI In Depth
Learning progress 0%
lesson text-processing regex awk

tr & Character Classes

5 min read Module 6: Text Processing: grep, sed & awk Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:33

What tr does

tr reads standard input and writes standard output. It takes a set of characters called SET1 and, depending on the mode, maps them to SET2, deletes them, or squeezes repeats. It does not take file names as arguments, so you always feed it through a pipe or a redirection. It does not know about lines either. It sees one character at a time, and a newline is just another character.

The mapping is positional. The first character of SET1 becomes the first character of SET2, and so on. If SET2 is shorter, its last character is repeated to fill the gap.

Translating characters

The basic form is tr SET1 SET2. Ranges such as a-z expand to the characters between the endpoints. The common use is changing case.

BASH1 lines
echo 'ello, World'| tr '-z''-Z'

The output is HELLO, WORLD. Punctuation and spaces are not in the range, so they pass through unchanged. Translating one character to another is also useful for swapping separators. For example, tr ':' ' ' turns colons into spaces.

Note

Use the named classes [:lower:] and [:upper:] when you want the letters of the current locale. A plain range such as a-z may behave differently in some locales, so the class form is the portable choice.

Character classes

A named class is written inside SET1 or SET2 as [:name:]. Common classes are [:alpha:] for letters, [:digit:] for digits, [:alnum:] for both, [:space:] for whitespace, [:punct:] for punctuation, and [:upper:] and [:lower:] for case. The brackets around the class are part of the class syntax, so in a shell command you still quote the whole set.

BASH1 lines
echo 'rder 66, ship 12!'| tr -cd ':digit:]'

The output is 6612. The -c option complements the set, so SET1 becomes every character that is not a digit, and -d deletes those characters. The result is the digits only, with no newline at the end because the input newline was also deleted. This complement-and-delete pattern is one of the most useful tr idioms.

Deleting and squeezing

-d deletes every character in SET1. -s squeezes runs of the same character in SET1 down to one. Squeezing is how you turn a line with irregular spacing into a single-space separated line, which makes cut and awk easier to use.

BASH1 lines
echo '    b   c'| tr -s ''

The output is a b c. Only the repeated spaces are reduced, and every other character is unchanged. Use [:space:] instead of a literal space when the input may contain tabs as well.

Working with newlines

tr is the standard way to turn a stream of words into one word per line, or the reverse. It is the step that the ranking pipeline in the sort unit relied on. Because tr sees the newline as a character, tr '\n' ' ' joins lines into a single line separated by spaces.

BASH2 lines
printf 'ne\ntwo\nthree\n'| tr '
''' echo

The output is one two three, followed by the newline from the separate echo command. The trailing newline of the original input became a space, so the line has no newline of its own until echo adds one.

Translating with a complement

The complement flag also works with translation. With -c and a SET2, every character outside SET1 is mapped to SET2. This is handy for replacing everything that is not a letter or digit with a single marker, such as an underscore, so that a messy label becomes a safe identifier.

BASH1 lines
echo 'y file (v2).txt'| tr -c ':alnum:]\n'''

The output is my_file__v2__txt. Every space, parenthesis, and dot became an underscore, and the letters and digits were left alone. The newline at the end of the input is in the set, so it is kept as well. Because the mapping is character by character, it is quick, but it cannot tell a dot that starts a file extension from a dot in the middle of a name.

Combine the flags carefully. Using -s with the same set squeezes the underscores that the translation produced, so my file name with two spaces becomes a single underscore rather than two. This pairing of translate and squeeze is a standard cleanup step before text is used in a file name or a database key.

Because tr works on one character at a time, it is also very cheap. It keeps no state between characters beyond the translation table it builds at startup, so it can process large streams with little overhead. That is one reason it appears in many shell pipelines, often right after a command that prints wide or messy text. A common order is to squeeze spaces with tr first, then let cut or awk split the fields, because the squeezed text has one predictable delimiter.

What tr cannot do

tr works on single characters and cannot match a sequence. It cannot replace the word cat with dog, because that requires a multi-character match, and tr would map the letters one by one. For that job use sed. The rule of thumb is simple: use tr when every change is a character-for-character change, and use sed or awk when you match text.

BASH1 lines
echo 'at'| tr 'at''og'

This prints dog, but only by accident of the positions. The tr command translated c to d, a to o, and t to g, one character at a time. Replacing cat wherever it appears in a longer line would need sed, which the next unit covers.

Terminal lab · 3 steps · real Linux in your browser

Translate, delete, and squeeze

Use tr with a range, a class, and squeeze.

01
Translate to uppercase
greet.txt holds a line of text. Convert its lowercase letters to uppercase: ``bash tr 'a-z' 'A-Z' < greet.txt > upper.txt ``
Show hint for step 1
tr reads standard input, so use < to feed it the file.
02
Keep only the digits
order.txt holds mixed text. Delete everything that is not a digit, using the complement flag with a character class: ``bash tr -cd '[:digit:]' < order.txt > digits.txt ``
Show hint for step 2
-c complements the set and -d deletes it.
03
Squeeze repeated spaces
messy.txt has runs of spaces between words. Reduce each run to one space: ``bash tr -s ' ' < messy.txt > tidy.txt ``
Show hint for step 3
-s squeezes repeats of the characters in the set.
Check yourself Question 1 of 4
Concept Check Where tr reads its input recall

Where does tr read its input from?

Recap & Key Takeaways

tr maps, deletes, or squeezes single characters from standard input, and it does not accept file names.
Named classes such as [:digit:], [:space:], and [:upper:] are portable, and -c with -d keeps or removes a class.
tr -s squeezes repeated characters, and tr with a newline argument joins or splits lines.
tr cannot match multi-character text; use sed or awk for that kind of replacement.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue