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:]'
Code:
HELLO WORLD
Convert to Lower Case
To convert text to lower case, use this command:
Code:
echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'
Code:
hello world
Delete Characters
To delete specific characters from the input, use the -d option:
Code:
echo "hello world" | tr -d 'l'
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 '
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'
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'
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'
Code:
HELLO WORLD
These are some of the basic and useful functions of the tr command in Linux.
Last edited: