tr command in UNIX
tr [OPTION]... SET1 [SET2]Introduction
Section titled “Introduction”Translate, squeeze, and/or delete characters from standard input, writing to standard output.
Options
Section titled “Options”The most useful options are:
-c,-C,--complement: use the complement ofSET1-d,--delete: delete characters inSET1(do not translate)-s,--squeeze-repeats: replace each sequence of a repeated character that is listed in the last specifiedSET, with a single occurrence of that character-t,--truncate-set1: first truncateSET1to length ofSET2
SETs are strings of characters. The most useful interpreted sequences are:
\NNN: character with octal valueNNN(1 to 3 octal digits)\\: backslash\b: backspace\n: new line\t: horizontal tab\v: vertical tabCHAR1-CHAR2: all characters fromCHAR1toCHAR2in ascending order[CHAR*]: inSET2, copies ofCHARuntil length ofSET1[:alnum:]: all letters and digits[:alpha:]: all letters[:blank:]: all horizontal whitespace[:digit:]: all digits[:graph:]: all printable characters, not including space[:lower:]: all lower case letters[:punct:]: all punctuation characters[:upper:]: all upper case letters[:xdigit:]: all hexadecimal digits[=CHAR=]: all characters which are equivalent toCHAR.
Please refer to the manual to see all the sequences.
However, translation occurs if -d is not given and both SET1 and SET2 appear.
The option -t may be used only when translating.
SET2 is extended to length of SET1 by repeating its last character as necessary.
Excess characters of SET2 are ignored.
Option -s uses the last specified SET, and occurs after translation or deletion.
Examples
Section titled “Examples”Here are some examples:
my_string="rock,paper,scissor"echo $my_stringrock,paper,scissor
echo $my_string | tr ',' ' 'rock paper scissor
# enclose comma sign between single quotes is not mandatory# echo $my_string | tr , '\n'echo $my_string | tr ',' '\n'rockpaperscissor
echo $my_string | tr -d ,rockpaperscissor
# transform ',' into '\n' and 'r' into 'X'echo $my_string | tr ',r' '\nX'XockpapeXscissoX
# transform lowercase letters to uppercaseecho $my_string | tr [:lower:] [:upper:]ROCK,PAPER,SCISSOR
# transform lowercase letters to uppercase and spaces to dashesecho " William Shakespeare" | tr '[:upper:] ' '[:lower:]-'-william-shakespeare
# override the variablemy_string="I am 20 years old"# transform digits to 'N'echo $my_string | tr [:digit:] 'N'I am NN years old
# transform all punctuation characters to '\'my_string="Hi! How are you? I'm 20 years old."echo $my_string | tr [:punct:] '\\'Hi\ How are you\ I\m 20 years old\
# HEX values are the digits from 0 to 9 and letters from 'A' (or 'a') to 'F' (or 'f')my_string="The color is: A3"# transform all hexadecimal characters to '@'echo $my_string | tr [:xdigit:] '@'Th@ @olor is: @@tr command works as well with files.
Lets’ download silvia.txt poem by William Shakespeare and put it in your /Desktop directory.
cd Desktop/# redirect standard input to 'silvia.txt' filetr [:lower:] [:upper:] < silvia.txtWHO IS SILVIA? WHAT IS SHE?THAT ALL OUR SWAINS COMMEND HER?HOLY, FAIR, AND WISE IS SHE;# ... (the rest of the file)
# redirect standard input to 'silvia.txt' file# and redirect standard output to 'uppercase-silvia.txt'tr [:lower:] [:upper:] < silvia.txt > uppercase-silvia.txtcat uppercase-silvia.txtWHO IS SILVIA? WHAT IS SHE?# ...Quotes
Section titled “Quotes”Manual reference: