Saturday, 19 July 2025

Five Different Uses of the tr Command in Unix/Linux

The tr (translate or delete characters) command in Unix/Linux is a powerful tool for basic text transformations. Below are five practical and commonly used examples.

1. Convert Lowercase to Uppercase

This is one of the most common uses of tr. It replaces all lowercase letters with uppercase:

echo "hello world" | tr 'a-z' 'A-Z'

Output: HELLO WORLD

2. Remove All Digits

You can delete characters using the -d option. This command removes all digits from a string:

echo "abc123def456" | tr -d '0-9'

Output: abcdeff

3. Replace Spaces with Newlines

This is useful for splitting words onto new lines:

echo "one two three" | tr ' ' '\n'

Output:

one
two
three
        

4. Squeeze Repeated Characters

The -s option squeezes sequences of a character into one:

echo "aaabbbcccaaa" | tr -s 'a'

Output: abbbccca

5. Remove Non-Alphabetic Characters

This command removes everything except letters:

echo "Hi #1! Welcome." | tr -cd '[:alpha:]'

Output: HiWelcome

\

No comments:

Post a Comment