tr Command: Tutorial & Examples

Manipulate standard input

The tr command, short for translate, is an essential utility in the shell command language. It's primarily used to translate or delete characters from standard input, writing to standard output.

How it works

The tr command works by taking two sets of characters as input. It replaces occurrences of the characters in the first set with the corresponding character in the second set. If the second set is shorter than the first, the tr command extends it by repeating its last character.

What it is used for

The tr command is versatile and is used for a variety of tasks in server and VM management. This includes translating lowercase letters to uppercase, deleting unwanted characters from output, and even replacing spaces with new lines. It's also handy for stripping out control characters, or for converting DOS/Windows text files to a Unix format.

Why it is important

The tr command is a powerful tool for manipulating text directly in the shell, without needing to use a text editing program. This can be particularly useful when processing large amounts of data or when you need to format data in a specific way for further processing.

How to use it and common command line parameters

Using the tr command is straightforward. You simply need to specify the set of characters to be replaced and the set of characters to replace them with.

Let's look at some examples:

echo 'Hello, World!' | tr '[:lower:]' '[:upper:]'

This command will output: 'HELLO, WORLD!'. It translates all lowercase letters to uppercase.

echo 'Hello, World!' | tr -d '[:punct:]'

This command will output: 'Hello World'. It deletes all punctuation characters.

There are many options that you can use with tr, including:

  • -d or --delete: delete characters in the first set
  • -s or --squeeze-repeats: replace each input sequence of a repeated character that is listed in the last specified SET with a single occurrence of that character
  • -c or --complement: use the complement of SET1

Potential problems and pitfalls

One common pitfall when using the tr command is assuming that it works with strings. However, tr works with sets of characters, not strings. For example, the command tr 'abcd' 'jklm' will replace each 'a' with a 'j', each 'b' with a ' k', and so on.

Also, when using tr, you should be aware that it reads from standard input and writes to standard output. This means that if you want to modify a file, you need to use redirection operators.

For example, to replace all lowercase letters with uppercase letters in a file, you would use:

tr '[:lower:]' '[:upper:]' < inputfile > outputfile

Typical problems that can be solved with the tr command

The tr command can help solve many text processing issues. For example, you could use tr to solve the following problems:

  • Replace all spaces with new lines to count the number of words in a file:

    tr ' ' '\n' < file | wc -l

  • Convert a file from DOS/Windows format to Unix format by removing carriage return characters:

    tr -d '\r' < inputfile > outputfile

  • Remove non-printable characters from a file:

    tr -cd '[:print:]' < inputfile > outputfile

In each of these cases, tr provides a quick and efficient way to process text directly from the command line.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA