tr
The tr (translate) command is used to translate, squeeze, or delete characters from standard input, writing to standard output. It is frequently used for changing case, replacing delimiters, or removing specific characters.
Basic Syntax
tr [OPTION]... SET1 [SET2]
Common Options
| Option | Description |
|---|---|
-d, --delete | Delete characters in SET1, do not translate. |
-s, --squeeze-repeats | Replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character. |
-c, -C, --complement | Use the complement of SET1. |
Real-world Examples
-
Convert lowercase to uppercase:
cat text.txt | tr 'a-z' 'A-Z' -
Replace spaces with underscores:
echo "hello world" | tr ' ' '_' -
Delete all numeric characters:
echo "password123" | tr -d '0-9' -
Squeeze multiple spaces into a single space:
echo "this has too many spaces" | tr -s ' '