Character-based translation using tr

Another interesting shell command is tr. This translates, squeezes, or deletes characters from the standard input. The syntax will be as follows:

tr [OPTION]... SET1 [SET2]

The options for the tr commands are explained in the following table:

Option

Description

-c, -C

Use complement of SET1

-d

This deletes a range of characters specified in SET1.

-s

This replaces consecutive multiple occurrences of characters in SET1 with a single occurrence.

-t

This truncates SET1 to the length of SET2. Any extra characters in SET1 will be not considered for translation.

SETs are a string of characters that can be specified using the following:

  • A character class: [:alnum:], [:digit:], [:alpha:] and so on
  • A character ...

Get Linux Shell Scripting Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.