tr Command in Linux



The tr command in Linux translates or deletes the characters from the standard input and writes the result to the standard output. It is often used in pipelines to clean up or modify input data.

Since tr processes standard input and outputs to standard output, it's commonly used with pipes (|) and redirects (>>) for handling file content.

Table of Contents

Here is a comprehensive guide to the options available with the tr command −

Syntax of tr Command

The syntax of the tr command in Linux is as follows −

tr [option]... string1 [string2]

In the above syntax, the [option] argument is used to specify the optional flags to modify behavior. The string1 argument is used to specify a set of characters to translate, delete, or squeeze.

The [string2] argument is used to mention the replacement characters that are required for translation, not for deleting or squeezing.

tr Command Options

The options of the Linux tr command are listed below −

Short Option Long Option Description
-c, -C --complement Use the complement of string1
-d --delete Delete characters in string1 without translating.
-s --squeeze-repeats Replace sequences of repeated characters listed in string1 with a single occurrence.
-t --truncate-set1 Truncate string1 to the length of string2.
--help Display help information and exit.
--version Output version information and exit.

In the above options list, the ARRAY1 and ARRAY2 signify arrays of characters.

Character Classes and Escapes Used in tr Command

A list of character classes and escapes that can be used with the tr command is listed below −

Syntax Description
\NNN Character with octal value NNN (1 to 3 octal digits)
\\ Backslash
\a Audible BEL
\b Backspace
\f Form feed
\n New line
\r Return
\t Horizontal tab
\v Vertical tab
CHAR1-CHAR2 All characters from CHAR1 to CHAR2 in ascending order
[CHAR*] In ARRAY2, copies of CHAR until length of ARRAY1
[CHAR*REPEAT] REPEAT copies of CHAR, REPEAT is octal if starting with 0
[:alnum:] All letters and digits
[:alpha:] All letters
[:blank:] All horizontal whitespace
[:cntrl:] All control characters
[:digit:] All digits
[:graph:] All printable characters, not including spaces
[:lower:] All lowercase letters
[:print:] All printable characters, including space
[:punct:] All punctuation characters
[:space:] All horizontal or vertical whitespace
[:upper:] All upper case letters
[:xdigit:] All hexadecimal digits
[=CHAR=] All characters which are equivalent to CHAR

Examples of tr Command in Linux

This section explores how to use the tr command in Linux with examples −

  • Translating Lowercase to Uppercase
  • Translating Uppercase to Lowercase
  • Deleting Characters
  • Replacing Characters
  • Squeezing Repeated Characters
  • Replacing Space with Newline
  • Replacing Newline with Space
  • Truncating a String
  • Complementing a String
  • Displaying Usage Help

Translating Lowercase to Uppercase

To translate the lowercase to uppercase, use the tr command in the following way −

tr a-z A-Z
type input here [Ctrl+D to end]

After executing the above command, type the string to be converted to uppercase −

tr Command in Linux1

Or, pipe the string using the echo command −

echo "tutorialspoint" | tr 'a-z' 'A-Z'
tr Command in Linux2

Translating Uppercase to Lowercase

To translate the uppercase to lowercase, use the tr command in the following way −

echo "TUTORIALSPOINT" | tr 'A-Z' 'a-z'
tr Command in Linux3

Deleting Characters

To delete characters, use the -d option. For example, the following command removes all the numeric characters.

echo "abc123" | tr -d '0-9'
tr Command in Linux4

Or −

echo "abc123" | tr -d '[:digit:]'
tr Command in Linux5

Replacing Characters

To replace characters from a string, use the following command −

echo "Akkle" | tr k p
tr Command in Linux6

Squeezing Repeated Characters

To squeeze the repeated characters, use the -s option −

echo "This    is    tutorialspoint" | tr -s '[:space:]'
tr Command in Linux7

Replacing Space with Newline

To replace space with a newline, use the tr command in the following way −

echo "Welcome to Tutorialspoint" | tr ' ' '\n'
tr Command in Linux8

Replacing Newline with Space

To replace a newline with a space, use the following command −

cat file.txt | tr '\n' ' '
tr Command in Linux9

Truncating a String

To truncate a string, use the -t option −

echo "abcdef" | tr -t 'abcdef' '123'
tr Command in Linux10

Complementing a String

To delete all characters except lowercase from a string of characters, use the -c option −

echo "pin code = 123" | tr -cd '0-9\n'
tr Command in Linux11

The above command deletes all characters except digits and newlines.

Displaying Usage Help

To display the usage help, use the --help option −

tr --help

Conclusion

The tr command in Linux is a simple yet powerful tool used to translate, delete, or squeeze characters from input data. It works with standard input and output. This tutorial explained the basic syntax of tr command, available options, and practical examples such as converting letter cases, removing characters, replacing or squeezing repeated characters, and more.

Advertisements