On Unix-like operating systems, the tr command automatically translates (substitutes, or maps) one set of characters to another.

This page covers the GNU/Linux version of tr.

Description

The tr utility copies the standard input to the standard output with substitution or deletion of selected characters.

  • Description
  • Syntax
  • How characters are specified
  • Environment
  • Exit status
  • Examples
  • Related commands
  • Linux commands help

Syntax

tr [-Ccsu] string1 string2

In this form, the characters in the string string1 are translated into the characters in string2 where the first character in string1 is translated into the first character in string2 and so on. If string1 is longer than string2, the last character found in string2 is duplicated until string1 is exhausted.

tr [-Ccu] -d string1

In this form, the characters in string1 are deleted from the input.

tr [-Ccu] -s string1

In this form, the characters in string1 are compressed as described for the -s option (see below).

tr [-Ccu] -ds string1 string2

In the fourth form, the characters in string1 are deleted from the input, and the characters in string2 are compressed as described for the -s option.

Options

How characters are specified

When specifying the characters to translate with tr, the following conventions are used to represent sets (or “classes”) of characters.

Any character not described by one of the following conventions represents itself.

Environment

The LANG, LC_ALL, LC_CTYPE and LC_COLLATE environment variables affect the execution of tr.

Exit status

tr returns an exit status of 0 if it operated successfully, and a value greater than zero if an error occurred.

Examples

tr -cs “[:alpha:]” “\n” < file1

Create a list of the words in file1, one per line, where a word is taken to be a maximal string of letters.

tr “[:lower:]” “[:upper:]” < file1

Translate the contents of file1 to uppercase.

tr -cd “[:print:]” < file1

Remove all non-printable characters from file1.

tr “[=e=]” “e”

Remove all “diacritical” marks from accented versions of the letter e.

ed — A simple text editor.sed — A utility for filtering and transforming text.sh — The Bourne shell command interpreter.