The tr command

The tr command is a Linux utility for text processing, such as translating, deleting, or squeezing repeated characters, which is shown as follows:

    $ tr '[a-z]' '[A-Z]' < filename

This will translate the lowercase characters to uppercase:

    $ tr '|' '~' < emp.lst

This will squeeze multiple spaces into a single space:

    $ ls -l | tr -s " "

In this example, the -s option squeezes multiple contiguous occurrences of the character into a single char. Additionally, the -d option can remove characters.

Get Learning Linux Shell Scripting - Second Edition 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.