Skip to content

tr command in MacOS

The tr command in MacOS is a powerful utility for translating, squeezing, and deleting characters. It can be used to transform text input by replacing specific characters with other characters or deleting them altogether. This command is commonly used in scripts to manipulate text data efficiently. With tr, users can perform a wide range of text transformations, such as converting uppercase letters to lowercase, removing specified characters, or even replacing whitespace with a specific character. Mastering the tr command can streamline text processing tasks and enhance automation workflows in MacOS.

tr Syntax:

Terminal window
tr [option] [set1] [set2]

MacOS tr Options:

OptionDescription
-dDelete characters in set1
-sSqueeze consecutive characters
-cComplement set1
-tReplace set1 with set2

tr Parameters:

ParameterDescription
set1Characters to translate or delete
set2Characters for replacement

How to use tr command:

Convert lowercase to uppercase

Terminal window
echo "hello world" | tr '[:lower:]' '[:upper:]'

Converts all lowercase letters to uppercase in the input string.

Remove specific characters

Terminal window
echo "Hello123" | tr -d '0-9'

Removes all digits from the input string.

Replace characters

Terminal window
echo "hello world" | tr 'l' 'L'

Replaces all occurrences of the letter ‘l’ with ‘L’.

Squeeze repeated characters

Terminal window
echo "hello world" | tr -s ' '

Squeezes repeated spaces in the input string to a single space.

Translate a range of characters

Terminal window
echo "abc123" | tr 'a-c' 'x-z'

Translates characters within the range ‘a’ to ‘c’ to the range ‘x’ to ‘z’.

Delete specific characters

Terminal window
echo "Hello123" | tr -d '[:digit:]'

Deletes all digits from the input string using the character class ’[:digit:]‘.

Complement characters

Terminal window
echo "hello world" | tr -C '[:lower:]' '[:space:]'

Complements characters outside of the set of lowercase letters and spaces.

Translate tab to space

Terminal window
echo -e "hello\tworld" | tr '\t' ' '

Translates tabs to spaces in the input string.

How do I use tr in MacOS?

To use the tr command in MacOS, execute the following command:

Terminal window
tr --option <value>

How can I delete characters using tr in MacOS?

To delete specific characters using the tr command in MacOS, use the following syntax:

Terminal window
echo "Hello, World!" | tr -d 'l'

How can I translate characters with tr in MacOS?

To translate characters using the tr command in MacOS, use a command similar to this:

Terminal window
echo "12345" | tr '123' 'abc'

How do I use the set1 and set2 options in tr on MacOS?

To specify the transformation sets in tr on MacOS, use the following syntax:

Terminal window
echo "hello" | tr 'el' 'nx'

How can I squeeze multiple characters into one using tr in MacOS?

To squeeze multiple characters into one using the tr command in MacOS, you can do the following:

Terminal window
echo "Hello World" | tr -s ' '

How do I use the complement option in tr on MacOS?

To complement a set of characters using the tr command in MacOS, use the following syntax:

Terminal window
echo "hello" | tr -c 'el' 'x'

How can I replace a specific range of characters using tr in MacOS?

To replace a range of characters using the tr command in MacOS, you can use a command like this:

Terminal window
echo "abcdef" | tr 'a-c' 'x'

How do I translate lowercase to uppercase characters using tr in MacOS?

To convert lowercase to uppercase characters using the tr command in MacOS, use this command:

Terminal window
echo "hello" | tr '[:lower:]' '[:upper:]'

How can I remove non-alphanumeric characters using tr in MacOS?

To remove non-alphanumeric characters using the tr command in MacOS, you can use the following syntax:

Terminal window
echo "Hello123!@#" | tr -cd '[:alnum:]'

Applications of the tr command

  • Character transformation
  • Text translation
  • Deleting characters
  • Squeezing repeated characters
  • Complementing character set