Introduction
The tr
command is a Linux command-line utility that translates or deletes characters from standard input (stdin
) and writes the result to standard output (stdout
). Use tr
to perform different text transformations, including case conversion, squeezing or deleting characters, and basic text replacement.
Since tr
can't read a file directly and outputs the results in standard output, it is often used with pipes (|
) and redirects (>>
) to allow more complex file content processing.
In this tutorial, you will learn to use the Linux tr
command.
Prerequisites
- A system running Linux.
- Access to the terminal (Ctrl + Alt + T).
Linux tr Command Syntax
The basic tr
command syntax is:
tr [options] SET1 [SET2]
Options
Running tr
without any options replaces each of the characters specified in SET1
with the characters from SET2
that have the same position.
For example:
In the example above, the echo command's output is piped into the tr
command, replacing each instance of e with o.
SET
SET
s are strings of characters. The command accepts the following interpreted sequences for character matching:
Sequence | Interpretation |
---|---|
\NNN | Characters with the NNN octal value (1 to 3 octal digits). |
\\ | Backslash. |
\a | An audible bell character. |
\b | Backspace. |
\f | Form feed. |
\n | Newline character. |
\r | Return character. |
\t | Horizontal tab. |
\v | Vertical tab. |
CHAR1-CHAR2 | All characters from CHAR1 to CHAR2 in an ascending order. |
[CHAR*] | Copies CHAR* in SET2 up to the length of SET1 . |
[CHAR*REPEAT] | Repeats copies of CHAR . Repeats octal if starting with 0. |
[:alnum:] | All letters and digits. |
[:alpha:] | All letters. |
[:blank:] | Horizontal whitespaces. |
[:cntrl:] | All control characters. |
[:digit:] | All digits. |
[:graph:] | Printable characters, excluding space. |
[:lower:] | All lowercase characters. |
[:print:] | Printable characters, including space. |
[:punct:] | All punctuation characters. |
[:space:] | Horizontal or vertical whitespace characters. |
[:upper:] | All uppercase letters. |
[:xdigit:] | Hexadecimal digits. |
[=CHAR=] | All characters equivalent to CHAR . |
Note: Another great Unix text stream editor is SED. The tool allows you to search, replace, add, and delete lines in a text file without opening it in a text editor.
Linux tr Command Options
The options provide additional character transformation actions. The available options are:
Option | Description |
---|---|
-C | Complements the characters in SET1 , including every character in the output except the ones specified. |
-c | Complements the values in SET1 . Operations apply to characters that are not in the given set. |
-d | Deletes characters from the SET1 input. |
-s | Squeezes repeated characters specified in the last operand (either SET1 or SET2 ) and replaces them with a single occurrence of that character. |
-t | Truncates SET1 to the length of SET2 . |
-u | Ensures that any output is unbuffered. |
--help | Displays the help file with all available options. |
--version | Displays the program version information. |
Linux tr Examples
Below are examples of using the tr
command with different options to transform input text.
Change Character Case
There are three ways of changing character case with tr
:
1. Specify the Exact Characters You Want Converted
Specify which characters from the input you want to convert. This option changes the character case or entire characters, replacing the ones from SET1
with the ones from SET2
.
For example:
2. Specify Character Range for Conversion
Specifying a range allows tr
to change the case of any character within that specified range. The following example shows how to convert uppercase characters to lowercase:
Press CTRL+C to exit input mode.
3. Specify Interpreted Sequences
Match and convert characters by specifying interpreted sequences. For example, the sequence for lowercase characters is [:lower:]
, and the sequence for uppercase characters is [:upper:]
. Specifying the two sequences instructs tr
to match and convert the character case:
The example above shows how tr
converts uppercase characters to lowercase.
Remove Repeated Characters
The -s
option squeezes repeated characters into a single instance of that character. The option is particularly useful when converting whitespaces to tabs or newline characters, and the input text contains multiple continuous whitespace characters.
For example, the following input contains multiple whitespace characters. Converting them to tabs without squeezing provides the following result:
To avoid multiple tabs and whitespaces in the output, specify the -s
option:
Delete Characters
Remove specific characters using the -d
option. In the following example, tr
deletes each instance of the e character:
Optionally, specify a group of characters using interpreted sequences. For example, remove all digits by specifying the [:digit:]
sequence:
Complement Sets
Use the -c
option to complement the characters in SET1
. In the following example, we remove all characters except digits:
All characters not in the specified set (in this case, everything except digits) are removed.
Remove Newline Characters
Shrink the space a text occupies by converting newline characters into spaces. The content then appears in a single line.
In the following example, we use the cat command to open a text file and pipe it into tr
to remove newline characters:
Redirect Into tr
An alternative to piping is to use redirection to feed content into tr
. Additionally, you can use redirection to save the tr
output in a file.
The following example shows how to remove all non-printable characters from a file whose contents are redirected into tr
:
Truncate Set
By default, if SET1
is longer than SET2
, tr
reuses the last character from SET2
when processing the input. For example:
Since SET1
is longer than SET2
, tr
reuses the last character from SET2
, in this case, 2.
Use the -t
option to truncate SET1
to the length of SET2
:
The -t
option instructs tr
to truncate SET1
to the length of SET2
and replaces only the first two characters.
Note: Learn how to truncate files in Linux.
Remove Diacritics
Use the [=CHAR=]
sequence to match all characters equivalent to the specified one. For example, the sequence can identify and remove diacritics from characters.
Print Each Word Separately
Print a file's contents line by line using the -c
option and replace the non-alphanumerical characters with a newline character.
Note: If you prefer using Bash, see how to read a file line by line.
The output is now displayed one word at a time.
Save Output to File
Since tr
doesn't change a file's contents directly, it also doesn't save any changes you make. Save the output to a file by redirecting it as shown in the following example:
Opening the new file with cat
shows that the changes have been saved.
Conclusion
This tutorial showed how to use the tr
command and its available options for various text transformations. Check out other Linux text utilities in our guides for the sort command and less terminal pager, or see the best Linux text editors.