VMware ESXi and vSphere Cluster Management

Change Characters with the Linux tr Command

Learn how to use the Linux tr command to translate and delete characters from standard input, files, and pipelines.

The Linux tr command translates, deletes, and can optionally squeeze individual characters in a text stream. It reads from standard input (stdin) and writes the transformed result to standard output (stdout).

tr works character by character. It is useful for changing letter case, replacing selected characters, or removing unwanted characters. It is not a word-replacement tool, a regular-expression substitution tool, or an in-place file editor.

What tr Does

A character translation replaces one input character with another according to a mapping. For example, a mapping from a to A changes every lowercase a in the input, wherever it occurs.

Unlike a string-oriented tool such as sed, tr does not replace complete words or phrases. It also does not modify a source file automatically. The command transforms a stream and sends the result to stdout.

  • Character translation: Replace individual characters according to a mapping.
  • Character deletion: Remove characters named in a set.
  • Stream processing: Read stdin and write stdout.
  • No automatic in-place editing: Save output separately if a transformed file is needed.

Command Syntax

tr [OPTIONS] SET1 [SET2]

SET1 is the source character set: the characters that tr searches for. SET2 is the replacement character set. Characters in corresponding positions are mapped together.

Options change the operation. The -d option, for example, deletes the characters listed in SET1 and does not use a replacement SET2.

Quote character sets so the shell passes them to tr as intended. Single quotes are generally a safe choice for literal sets and POSIX character classes.

Prepare and Inspect Example Input

Assume a file named tr_example contains several short sentences with repeated lowercase letters:

This is a small text file.
Each sentence has letters.
A few letters appear again.

Inspect the file with:

cat tr_example

This displays the original text unchanged. The cat command is only being used to inspect the file; it is not part of the translation.

Reading a File with Input Redirection

The input redirection operator < supplies a file's contents to a command as standard input:

tr 'a' 'A' < tr_example

The shell reads tr_example and provides its contents to tr through stdin. tr writes the transformed text to stdout, so the result appears in the terminal.

Input redirection does not edit tr_example. The source file remains unchanged unless you deliberately save a transformed result and later replace the original.

Output can be handled in several ways:

  • Display it: Run the command without output redirection.
  • Save it: Use > to write stdout to a new file.
  • Process it further: Pipe stdout to another command with |.

Translate One Character

To replace every lowercase a with uppercase A, run:

tr 'a' 'A' < tr_example

Every matching lowercase a is translated independently throughout the input. Other letters, spaces, digits, and punctuation remain unchanged.

This is a character mapping, not a search-and-replace operation for the word or string a. Since the target is one character, each occurrence is handled separately.

Translate Multiple Characters

Multiple source and replacement characters are mapped by position:

tr 'ae' 'AE' < tr_example

The first character in SET1 maps to the first character in SET2, and the second maps to the second:

SET1 positionSET2 positionTranslation result
First: aFirst: AEvery a becomes A.
Second: eSecond: EEvery e becomes E.
Any character not targeted by SET1No matching mappingIt remains unchanged.

Keep the sets aligned in the order you intend. For example, 'ae' and 'EA' would map a to E and e to A, which is usually not the intended result.

Convert Lowercase Letters to Uppercase

tr supports POSIX character classes. A POSIX character class is a named group of characters written inside brackets, such as [:lower:] or [:upper:]. In tr, use the complete bracketed form:

tr '[:lower:]' '[:upper:]' < tr_example

This translates all lowercase alphabetic characters to their uppercase equivalents. Spaces, punctuation, digits, and characters that are already uppercase remain unchanged.

The two classes are aligned alphabetically by their character ordering, allowing lowercase letters to map to the corresponding uppercase letters. Quoting prevents the shell from interpreting the bracket characters before tr receives them.

Delete Characters with -d

Use -d to delete every character named in SET1:

tr -d 'a' < tr_example

All lowercase a characters are removed. There is no replacement SET2 because deletion needs only the set of characters to remove.

Deletion is useful for stripping unwanted punctuation or removing particular control characters. It can also be used for line-ending cleanup in appropriate input formats, but inspect the data carefully because deleting a character removes every occurrence of it.

Save Transformed Output Safely

Use output redirection to save the result in a separate file:

tr '[:lower:]' '[:upper:]' < tr_example > tr_example_uppercase

The transformed text is written to tr_example_uppercase, while tr_example remains intact. Review the new file before deciding whether it should replace the original.

tr 'a' 'A' < tr_example > tr_example

The shell opens the output file before tr starts reading stdin. Opening it for > normally truncates it, potentially making the input empty before processing begins. Always write to a different destination or temporary file.

Use tr in a Pipeline

A pipeline sends one command's stdout to another command's stdin. This example supplies text with printf and converts it to uppercase:

printf '%s\n' 'This is a text.' | tr '[:lower:]' '[:upper:]'

The output is:

THIS IS A TEXT.

This demonstrates that tr does not require a file. Any command that writes suitable text to stdout can provide its input.

Common tr Operations

TaskOption or syntaxExampleEffect
Translate one characterTwo one-character setstr 'a' 'A' < tr_exampleChanges every lowercase a to A.
Translate multiple charactersPositionally aligned setstr 'ae' 'AE' < tr_exampleChanges a to A and e to E.
Convert lowercase to uppercasePOSIX character classestr '[:lower:]' '[:upper:]' < tr_exampleChanges all lowercase alphabetic characters to uppercase.
Delete characters-d SET1tr -d 'a' < tr_exampleRemoves every lowercase a.
Read from a fileInput redirectiontr 'a' 'A' < tr_exampleProvides the file contents through stdin.
Save outputOutput redirectiontr 'a' 'A' < tr_example > resultWrites transformed text to a separate file.

Character Set Mapping Rules

SET1 positionSET2 positionTranslation result
First characterFirst characterThe first source character maps to the first replacement character.
Second characterSecond characterThe second source character maps to the second replacement character.
Character absent from SET1No target positionThe input character is not translated and remains unchanged.

Troubleshooting

The source file did not change

This is expected when using input redirection. tr writes to stdout and does not edit the input file in place. Redirect the output to a separate file, inspect it, and replace the original only if appropriate.

The source file became empty

This usually happens when the same filename is used on both sides of redirection. The shell truncates the output file before tr reads it. Use a different temporary or destination filename.

Only some intended characters changed

Check the order and length of SET1 and SET2. Translation is positional, so each source character must be placed opposite its intended replacement.

The shell changes the character set

Special shell characters may be expanded or interpreted before tr runs. Quote both sets, preferably with single quotes:

tr '[:lower:]' '[:upper:]' < tr_example

I need to replace a complete word

tr operates on individual characters, not multi-character words or phrases. Use a string-oriented stream editor such as sed when whole-word or phrase substitution is required.

Punctuation and digits were not converted

The [:lower:] class contains lowercase letters only. Leaving punctuation, spaces, digits, and already-uppercase letters unchanged is the expected behavior.

Exam-Ready Notes

  • tr reads standard input and writes standard output.
  • The general form is tr [OPTIONS] SET1 [SET2].
  • SET1 contains characters to find; SET2 contains corresponding replacements.
  • Mappings are positional: first to first, second to second, and so on.
  • tr -d 'x' deletes every x and does not need SET2.
  • < supplies a file as stdin; > saves stdout to a file.
  • Input redirection alone never changes the source file.
  • Never redirect output to the same file used as input.
  • Use '[:lower:]' and '[:upper:]' for alphabetic case conversion.
  • Quote character sets to protect them from shell interpretation.