tr

>Usage

tr (Translate) is a Unix command-line tool used for translating, deleting, and squeezing characters from standard input. It's great for quick text transformations, such as converting lowercase to uppercase or removing unwanted characters.

>Synopsis

tr [option] [string]

>Options

-d

-d (delete characters): Deletes the specified characters from the input.

Example 1:

Dots

-zsh

> echo "Hello World" | tr -d "o"


Hell Wrld

-s

-s (squeeze repeated characters): Replaces sequences of repeated characters with a single instance.

Example 2:

Dots

-zsh

> echo "Hellooo World" | tr -s "o "


Hello World

-c

-c (complement): Matches and operates on all characters except the specified set.

Example 3:

Dots

-zsh

> echo "123abc" | tr -c "a-z" "*"


***abc*%

[:upper:] and [:lower:]

[:upper:] and [:lower:] (case conversion): Converts all uppercase letters to lowercase, or vice versa.

Example 4:

Dots

-zsh

> echo "Hello World" | tr '[:lower:]' '[:upper:]'


HELLO WORLD

-t

-t (truncate set1): Truncates the first set to the length of the second set.

Example 5:

Dots

-zsh

> echo "abcdef" | tr -t "abc" "XYZ"


XYZdef

Character Ranges

Character Ranges (e.g., a-z, 0-9): Specifies a range of characters to translate.

Example 6:

Dots

-zsh

> echo "abcdef" | tr "a-f" "1-6"


123456

-D

-D (delete non-matching characters): Specifies a range of characters to translate.

Example 7:

Dots

-zsh

> echo "Unix123" | tr -D "a-zA-Z"


Unix

Translating Tabs to Spaces

Translating Tabs to Spaces: Replaces tabs with spaces for formatting text.

Example 8:

Dots

-zsh

> echo -e "Column1\tColumn2" | tr "\t" " "


Column1 Column2

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Luca Fusetti