Replacing/Removing Characters in a File or Pipeline

The tr command enables you to translate every occurrence of one specific character into a different character. It also enables you to delete every occurrence of a specific character. tr does not allow any files on the command line, so you must pipe any data for tr to translate.

Determining Which Type of tr Is on Your UNIX System

Unfortunately, there are two different versions of tr. They differ in whether they require brackets [ ] around ranges of characters. Some versions of UNIX have one type of tr; other versions have a different type of tr.

This is how the nonbracket version of tr works:

$ echo abcdefaabbcc | tr 'a-c' '1-3'

In this command, echo sends the letters abcdefaabbcc ...

Get Practical UNIX now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.