Skip to content

tr Command: Translate or Delete Characters

Summary

The tr command in Linux is a versatile utility for translating, squeezing, and/or deleting characters from standard input and writing the result to standard output. It's often used for simple text manipulation tasks.

Introduction

The tr command, short for "translate," provides a way to perform character-based transformations. It operates by replacing characters in the input stream based on a set of rules defined by you. This makes it ideal for tasks like converting text to uppercase or lowercase, deleting specific characters, or replacing one set of characters with another. tr reads from standard input and writes to standard output, so it's often used in pipelines.

Use case and Examples

Convert to Uppercase

echo "hello world" | tr '[:lower:]' '[:upper:]'
This example converts all lowercase characters in the string "hello world" to uppercase, resulting in "HELLO WORLD". The POSIX character class [:lower:] matches all lowercase letters, and [:upper:] matches all uppercase letters.

Delete Characters

echo "this is a string with numbers 12345" | tr -d '0-9'
This example deletes all digits from the input string. The -d flag tells tr to delete the specified characters. The range 0-9 specifies all numerical digits. The output would be "this is a string with numbers ".

Squeeze Repeating Characters

echo "this   has   multiple   spaces" | tr -s ' '
This example squeezes multiple consecutive spaces into a single space. The -s flag tells tr to squeeze repeating characters specified in the first set. The output would be "this has multiple spaces".

Replace Characters

echo "replace a with b" | tr 'a' 'b'
This example replaces all occurrences of the character 'a' with the character 'b'. The output is "replbce b with b".

Complement and Delete

echo "abcdefg12345" | tr -dc 'a-z\n'
This command utilizes -d to delete and -c to take the complement of a-z\n. It effectively removes everything except lowercase letters and newline characters, printing abcdefg.

Commonly used flags

Flag Description Example
-d Delete characters specified in SET1. echo "hello123world" | tr -d '0-9' (Deletes all digits)
-s Squeeze repeating characters specified in SET1, replacing each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character. echo "a lot of spaces" | tr -s ' ' (Reduces multiple spaces to single spaces)
-t Truncate SET1 to the length of SET2 first. echo "abcde" | tr -t 'abc' '12' (truncate abc to ab, then translate a to 1 and b to 2, output is 12cde)
-c or --complement Use the complement of SET1. This means tr will operate on all characters not in SET1. echo "abcdefg123" | tr -c 'a-z' '*' (Replaces everything that is not a lowercase letter with '*')
--help Display a help message and exit. tr --help
--version Output version information and exit. tr --version


Share on Share on

Comments