the tr command stands for 'translate characters'. It is used to replace or delete characters.
tr
command structure:
tr [OPTION] SET1 [SET2]
The following options are available:
C
, c
to use the complement of SET1
d
, --delete
. to delete characters in SET1, do not translate
s
, --squeeze-repeats
to replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
To learn how to use the tr
command, let's create a simple file named 'cousins.txt' using the cat
command in a terminal window :
cat > cousins.txt << EOF
Luka m 14
Mathias m 11
Jules m 11
Maxime m 9
Eloise f 5
Thibaud m 3
Nina f 11
Zoe f 15
Gaspard m 6
EOF
To submit "cousins.txt" to the tr
command you can proceed in two ways refer to the Unix Command page:
cat cousins.txt | tr [OPTION] SET1 [SET2]
tr [OPTION] SET1 [SET2] < cousins.txt
Reminder : for more details on predefined class refer to the Regular Expressions page
To convert lower case alphabetic characters to upper case alphabetic characters:
tr [:lower:] [:upper:] < cousins.txt
LUKA M 14
MATHIAS M 11
JULES M 11
MAXIME M 9
ELOISE F 5
THIBAUD M 3
NINA F 11
ZOE F 15
GASPARD M 6
or either:
tr a-z A-Z < cousins.txt
LUKA M 14
MATHIAS M 11
JULES M 11
MAXIME M 9
ELOISE F 5
THIBAUD M 3
NINA F 11
ZOE F 15
GASPARD M 6
tr [:blank:] ';' < cousins.txt
Luka;;;;m;;;;;14
Mathias;m;;;;;11
Jules;;;m;;;;;11
Maxime;;m;;;;;;9
Eloise;;f;;;;;;5
Thibaud;m;;;;;;3
Nina;;;;f;;;;;11
Zoe;;;;;f;;;;;15
Gaspard;m;;;;;;6;
tr -s [:blank:] ';' < cousins.txt
Luka;m;14
Mathias;m;11
Jules;m;11
Maxime;m;9
Eloise;f;5
Thibaud;m;3
Nina;f;11
Zoe;f;15
Gaspard;m;6;
tr -s [:blank:] ' ' < cousins.txt
Luka m 14
Mathias m 11
Jules m 11
Maxime m 9
Eloise f 5
Thibaud m 3
Nina f 11
Zoe f 15
Gaspard m 6
tr -d 'a,k' < cousins.txt
Lu m 14
Mthis m 11
Jules m 11
Mxime m 9
Eloise f 5
Thibud m 3
Nin f 11
Zoe f 15
Gsprd m 6
tr -d [:digit:] < cousins.txt
Luka m
Mathias m
Jules m
Maxime m
Eloise f
Thibaud m
Nina f
Zoe f
Gaspard m
-c
optiontr -cd [:digit:] < cousins.txt
14111195311156