The tr (translate) command in Linux reads text from standard input, performs some modification to the text, and then sends it to standard output. This article explains and shows why and how you might use it.
tr Command Syntax
Here’s the syntax you’ll need to use the command:
tr OPTIONS SET1 [SET2]
Note that:
- OPTIONS should be a list of options from the below table
- The characters in SET1 will be replaced with the characters in the corresponding position in SET2
- These are optional – some of the OPTIONS are capable of performing translations, so both of the SETs aren’t necessarily required.
- You’ll notice the lack of text being processed – this is supplied using standard redirection – more on this in the examples below.
Here are the available OPTIONS – straight from the user manual:
OPTIONS | |
---|---|
-c | Use the complement of SET1 |
-d | Delete characters in SET1, do not translate |
-s | Replace each sequence of a repeated character that is listed in the last specified SET with a single occurrence of that character |
-t | First, truncate SET1 to length of SET2 |
If you need to replace or delete special characters, like newlines and tabs in the SETs, you can use the following sequences:
Interpreted sequence | Special character |
---|---|
\NNN | Character with octal value NNN (1 to 3 octal digits) |
\\ | Backslash |
\b | Backspace |
\n | New line |
\r | Return |
\t | Horizontal tab |
[:alnum:] | All letters and digits |
[:alpha:] | All letters |
[:blank:] | All horizontal whitespace |
[:digit:] | All digits |
[:lower:] | All lower case letters |
[:space:] | All horizontal or vertical whitespace |
[:upper:] | All upper case letters |
I’ve only included some commonly used options/sequences above (as there are quite a few). You can view a full list of command options in the user manual, which can be found by running:
man tr
tr Command Examples
Here are a few examples of how to use the tr command.
These are only examples; you can mix-and-match options and sequences to get your own desired results.
Removing Characters
The below example removes specified characters from a string. The output of the echo command is piped to the tr command.
This is how the tr command receives the text you wish to translate – it must be supplied as standard input via piping or redirection.
echo "axbxcxdx" | tr -d 'x'
Above, the -d option instructs tr to delete the character x from the output of the echo command.
This will return:
abcd
lowercase to UPPERCASE
The below example uses the upper/lower interpreted sequences to replace all lowercase characters with uppercase characters:
echo "aBcDeFg" | tr '[:lower:]' '[:upper:]'
This will output:
ABCDEFG
Replace Characters
tr is probably most commonly used to replace characters in a string – for example, replacing whitespace with dashes:
echo "frogs and fish" | tr '[:blank:]' '-'
Which will output:
frogs-and-fish
This example uses two SETs – the first set contains the identifier for white/blank space (i.e., spaces tabs), the second contains a dash – so the whitespace is replaced with dashes (SET2 replacing SET1).
This can be useful when generating filenames for uploading to a website and making sure there are no spaces in those filenames.
Replacing Multiple Characters
In the last example, each set contains only a single character. Multiple characters can be supplied to do multiple replacements at once:
echo "frogs and fish" | tr 'sf' 'SF'
Which will output:
FrogS and FiSh
As you can see, the corresponding letters in the first were substituted with those from the second.
Removing Repeated Characters
Below, unneeded repeated whitespace is removed from some text:
echo 'Absolutely spaced out' | tr -s '[:space:]'
Which will output:
Absolutely spaced out
The [:blank:] identifier was used with the -s option to identify and remove repeating whitespace.