The tr command (short for translate) is used to translate or delete characters. It operates character by character.
Consider demo.txt with data
While Learning Linux, It Is Not Required To Eat Anything.
$ tr 'aeiou' 'AEIOU' < demo.txt- Replaces all lowercase vowels with their uppercase counterparts.
- Output:
WhIlE lEArnIng UnIx nOt rEqUIrEd tO EAt.
$ tr '[a-z]' '[A-Z]' < demo.txt- Converts all lowercase letters to uppercase.
- Output:
WHILE LEARNING UNIX NOT REQUIRED TO EAT.
$ tr '[a-z][A-Z]' '[A-Z][a-z]' < demo.txt- Converts lowercase letters to uppercase and vice versa.
- Output:
Creates a new file:$ tr '[a-z][A-Z]' '[A-Z][a-z]' < demo.txt > temp.txt
$ tr 'aeiou' '7' < demo.txt- Replaces all lowercase vowels with the digit
7.
emp.dat File Content:
eno|ename|esal|eaddr|dept|gender
100|sunny|1000|mumbai|admin|female
200|bunny|2000|chennai|sales|male
300|chinny|3000|delhi|accounting|female
400|vinny|4000|hyderabad|admin|male
500|pinny|5000|mumbai|sales|female
$ tr '|' '\t' < emp.dat- Replaces the
|symbol with a tab.
$ tr '|' ':' < emp.dat- Replaces the
|symbol with a colon.
$ tr -d 'a' < demo.txt- Deletes all occurrences of the letter
a.
$ tr -d 'aeiou' < demo.txt- Deletes all lowercase vowels.
$ tr -s 'a' < demo.txt- Replaces sequences of repeated
as with a singlea. -sOption: Squeezes repeated characters into a single occurrence.
The tr command is highly versatile for text manipulation tasks like replacing, deleting, and compressing characters.