Trade, replace, or change characters using the TR command in Linux

Home > Search > Linux commands
  by

The tr command without any options will "trade" old characters for new characters. For example, to replace H with A:

[user1@server1 ]# echo "Hello World!" | tr H A
Aello World!

 


Change every character

The [:graph:] option can be used to change every alphabetic, numberic, and special character, but not spaces. In this example, Hello 123! is changed to AAAAA AAAA.

[user1@server1 ]# echo "Hello 123!" | tr [:graph:] A
AAAAA AAAA

 


Change every alphabetic and numeric character

The [:alnum:] option can be used to change every alphabetic and numberic character, but not special characters. In this example, Hello 123! is changed to AAAAA AAA!.

[user1@server1 ]# echo "Hello 123!" | tr [:alnum:] A
AAAAA AAA!

 


Change every alphabetic character

The [:alpha:] option can be used to change every alphabetic character, but to not change numeric characters. In this example, Hello 123! is changed to AAAAA 123!.

[user1@server1 ]# echo "Hello 123!" | tr [:alpha:] A
AAAAA 123!

 


Change every numeric character

The [:digit:] option can be used to change every numeric character, but to not change alphhabetic characters. In this example, Hello 123! is changed to Hello AAA!.

[user1@server1 ]# echo "Hello 123!" | tr [:alpha:] A
Hello AAA!

 


Change every blank space

The [:blank:] option can be used to change every blank space. In this example, the blank space in Helo World is changed to a new line.

[user1@server1 ]# echo "Hello World!" | tr [:blank:] '\n'
Hello
World!

 


Change every character except for specified characters

The -c or --complement option can be used to change every character except for the character specified. In this example, the H character is not changed, and everything else will be changed to an A.

[user1@server1 ]# echo "Hello World!" | tr -c H A
HAAAAAAAAAAA

 


Consolidate multiple occurences of the same character

The -s or --squeeze-repeats option can be used to squeeze multiple occurences of the same character into one occurence. In this example, instead of replacing AAAAA to BBBBB, AAAAA is replaced to just B.

[user1@server1 ]# echo "AAAAA" | tr -s A B
B

 


Delete characters

The -d or --delete option can be used to remove certain characters. For example, to remove the letter l in Hello World:

[user1@server1 ]# echo "Hello World!" | tr -d l
Heo Word!

 

Spaces can be removed. Foe example, to remove the space between Hello World:

[user1@server1 ]# echo "Hello World!" | tr -d ' '
HelloWorld!

 

New lines can be removed.

[user1@server1 ]# tr -d '\n' < file1
Hello World!How are you today?

 


Make all characters upper case or lower case

The [:lower:] and [:upper:] options can be used to make all characters upper case or lower case.

[user1@server1 ]# echo "Hello World!" | tr [:lower:] [:upper:]
HELLO WORLD!

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments