The tr
provides a means of translating (or transliterating) characters in
text for simple search and replace operations.
The call signature is:
tr {option} {string1} {string2}
where string1 defines the array of characters to search for, and string2 defines the array of characters to replace those characters with.
The following example details the basic behavior. Starting with our colors.csv file:
suppose we want to adjust the colors slightly. The following command changes each occurrence of "f" with "c", and each occurrence of "0" with "4":
When a character from string1 is encountered, it is replaced with the character at the same index of string2.
As such, string1 and string2 should generally be the same length. Cases where they are not are treated as follows:
-
When string1 is longer than string2 and the
-t
option is not specified, the last character of string2 is repeated to achieve the same length as string1. -
When string1 is longer than string2 and the
-t
option is specified, then string1 is truncated to achieve the same length as string2. -
When string2 is longer than string1, string2 is truncated to achieve the same length as string1.
string1 and string2 both support character ranges. For example, the following converts characters from lower-case to upper-case:
Deleting Characters
The tr
command can also be used to delete unwanted characters from the input stream.
This is achieved by specifying the -d
option and omitting string2. For example, to
remove "#" from the output once can execute:
Squeezing Characters
Finally, the tr
command can also "squeeze" adjacent runs of "E", "L", or "C" down to a
single character, using the -s
option, as follows: