Change and Delete Characters with the Linux tr Command
Learn how to use Linux tr to translate, convert, delete, and squeeze characters with standard input, redirection, and pipelines.
The Linux tr command is a text-processing filter that works character by character. It can translate one character into another, map several characters to replacements, delete selected characters, and optionally squeeze repeated characters. It reads from standard input and writes the transformed result to standard output.
tr is designed for individual characters and character classes, not word-level replacement. For example, it can change every lowercase a into uppercase A, but it is not the right tool for replacing one complete word with another.
Standard input, standard output, and tr
Standard input (stdin) is the input stream a command reads. It can come from keyboard input, a file supplied with input redirection, or the output of another command in a pipeline. Standard output (stdout) is the stream where a command writes its result.
tr normally does not accept a filename as a positional input argument. Provide file content with <, or pipe content into the command.
tr 'a' 'A' < tr_example
printf '%s\n' 'sample text' | tr '[:lower:]' '[:upper:]'
Without redirection or a pipeline, tr can read from the keyboard. Type input, press Enter, and finish the input stream with the terminal's end-of-file keystroke, usually Ctrl-D on Linux.
Command syntax
tr [OPTIONS] SET1 [SET2]
| Element | Meaning | Example |
|---|---|---|
OPTIONS | Changes the operation, such as deleting characters. | -d |
SET1 | The source characters or character class to examine. | 'a' |
SET2 | The replacement characters used for translation. | 'A' |
| stdin input | The text read by tr. | < tr_example |
| stdout output | The transformed text produced by tr. | > tr_output |
For ordinary translation, SET1 and SET2 are used together. With -d, SET1 identifies characters to delete and SET2 is omitted.
Prepare a demonstration file
The examples below use a file named tr_example. It can contain several sentences, including lowercase a characters and braces, for example:
This is a small sample.
A {simple} example has data.
Read the file with a command such as cat tr_example if you want to inspect its original contents before transforming it.
Translate one character
To change every lowercase a into uppercase A, use:
tr 'a' 'A' < tr_example
Every matching input character is translated independently. An a in the middle of a word, at the beginning of a word, or next to punctuation is treated the same way. Characters that are not in SET1 pass through unchanged.
| Task | Option or character sets | Result |
|---|---|---|
| Translate one character | 'a' 'A' | Every lowercase a becomes uppercase A. |
| Translate multiple characters | Paired SET1 and SET2 | Each source character maps to the replacement at the same position. |
| Convert lowercase to uppercase | '[:lower:]' '[:upper:]' | Lowercase characters are converted to uppercase characters. |
| Delete characters | -d 'SET1' | Every character in SET1 is removed. |
Translate multiple characters with positional mapping
When both sets contain several characters, tr maps characters by position. The first character in SET1 maps to the first character in SET2, the second maps to the second, and so on.
tr '{}' '()' < tr_example
This command maps { to ( and } to ). Thus, text such as {simple} becomes (simple).
Set order matters. If the characters are listed in the wrong order, the mappings will also be wrong. For predictable results, use short, explicit sets when you are learning or testing a transformation.
Convert case with character classes
A character class is a named collection of characters. The classes [:lower:] and [:upper:] represent lowercase and uppercase characters. In tr, character classes are commonly enclosed in brackets when passed as arguments:
tr '[:lower:]' '[:upper:]' < tr_example
This converts lowercase letters to their corresponding uppercase letters. Character classes are locale-aware: the characters recognized by a class can depend on the current locale. If a conversion does not cover expected non-ASCII characters, check the active locale and test representative input.
printf '%s\n' 'sample text' | tr '[:lower:]' '[:upper:]'
The pipeline uses | to send the output of printf to tr's standard input.
Delete characters with -d
The -d option deletes every occurrence of the characters specified by SET1. Do not provide SET2 for this operation.
tr -d 'a' < tr_example
The command removes all lowercase a characters from the input. It does not remove uppercase A, because the two characters are distinct.
Save output safely
tr writes transformed text to standard output. It does not modify the source file automatically. Use output redirection to save the result:
tr 'a' 'A' < tr_example > tr_output
The original tr_example remains unchanged, while the translated text is stored in tr_output.
What tr can and cannot do
trchanges characters wherever they occur, including inside words.- It does not normally perform word replacement, pattern matching, or word-boundary matching.
- Use a word-oriented tool such as
sed,awk, or a scripting language when the task needs complete words, regular expressions, or more complex conditions. - In addition to translation and deletion, implementations commonly support squeezing repeated characters with
-s. For example,tr -s ' 'reduces consecutive spaces to one space.
Troubleshooting
Using a filename as an argument
A command such as tr 'a' 'A' tr_example does not tell tr to read that file in the usual Unix filter model. It may report an extra operand or continue waiting for standard input. Use tr 'a' 'A' < tr_example, or pipe another command's output into tr.
Unexpected character mappings
Check that SET1 and SET2 have the intended order and corresponding lengths. The mapping is positional, so a reordered set changes the result.
Case conversion misses expected characters
Character classes depend on the current locale. Check the locale configuration and test the specific characters involved when processing non-ASCII text.
Only complete words should change
Because tr operates on characters, it will also change matching characters inside words. Choose sed, awk, or another word-aware tool when the replacement requires word boundaries or patterns.
Key points
- The general form is
tr [OPTIONS] SET1 [SET2]. SET1identifies source characters;SET2identifies replacement characters.- Use
<for file input and|for pipeline input. - Use
-dto delete characters, withSET2omitted. - Use
>to save output to a different file. - Remember that
tris character-oriented, not word-oriented.