tr


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:

·
·
ninja$: cat colors.csv
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
ninja$:  

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":

·
·
ninja$: cat colors.csv | tr f0 c4
red,#c44
green,#4c4
blue,#44c
yellow,#cc4
cyan,#4cc
magenta,#c4c
ninja$:  

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:

  1. 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.

  2. When string1 is longer than string2 and the -t option is specified, then string1 is truncated to achieve the same length as string2.

  3. 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:

·
·
ninja$: cat colors.csv | tr f0 c4 | tr a-z A-Z
RED,#C44
GREEN,#4C4
BLUE,#44C
YELLOW,#CC4
CYAN,#4CC
MAGENTA,#C4C
ninja$:  

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:

·
·
ninja$: cat colors.csv | tr f0 c4 | tr a-z A-Z | tr -d #
RED,C44
GREEN,4C4
BLUE,44C
YELLOW,CC4
CYAN,4CC
MAGENTA,C4C
ninja$:  

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:

·
·
ninja$: cat colors.csv | tr f0 c4 | tr a-z A-Z | tr -d # | tr -s ELC
RED,C44
GREN,4C4
BLUE,44C
YELOW,C4
CYAN,4C
MAGENTA,C4C
ninja$: