Using tr

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,067
Reaction score
2,794
Credits
26,712

Converting Text to Upper and Lower Case​

You can use the text replace command.

Convert to Upper Case​

To convert text to upper case, you can use the following command:
Code:
 echo "hello world" | tr '[:lower:]' '[:upper:]'
This command will output:
Code:
 HELLO WORLD

Convert to Lower Case​

To convert text to lower case, use this command:
Code:
 echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'
This command will output:
Code:
 hello world

Delete Characters​

To delete specific characters from the input, use the -d option:
Code:
 echo "hello world" | tr -d 'l'
This command will output:
Code:
 heo word

Squeeze Repeated Characters​

To replace sequences of repeated characters with a single character, use the -s option:
Code:
 echo "hellooo world" | tr -s 'lo '
This command will output:
Code:
 helo world

Replace Characters​

To replace specific characters with others, you can specify two sets of characters:
Code:
 echo "hello world" | tr 'h' 'H'
This command will output:
Code:
 Hello world

Complement Characters​

To replace all characters that are not in the specified set, use the -c option:
Code:
 echo "hello world" | tr -c 'a-z' 'X'
This command will output:
Code:
 helloXworld

Using Character Ranges​

You can also use character ranges to specify sets of characters:
Code:
 echo "hello world" | tr 'a-z' 'A-Z'
This command will output:
Code:
 HELLO WORLD

These are some of the basic and useful functions of the tr command in Linux.
 
Last edited:
Top