Name
tr
Synopsis
tr [options
] [string1
[string2
]]
Copy standard input to standard output, performing substitution of characters from string1 to string2 or deletion of characters in string1. Some older System V systems require that string1 and string2 be enclosed in square brackets. (This is true of Solaris’s /usr/bin/tr, for example.) Most other versions do not have this requirement. POSIX-compliant versions do not have this requirement either.
Common Options
-c
,--complement
Complement characters in string1 with characters in the current character set. The complement is the set of all characters not in string1. This option works in terms of byte values.
-d
,--delete
Delete characters in string1 from output.
-s
,--squeeze-repeats
Squeeze out repeated output characters in string2.
Solaris and Mac OS X Option
-
-C
Like
-c
, but work in terms of characters, which may be multibyte values, depending upon the local character set.
GNU/Linux Option
-t
,--truncate-set1
Truncate string1 to the length of string2 before processing the input.
Mac OS X Option
-
-u
Force output to be unbuffered.
Examples
Change uppercase to lowercase in a file:
tr 'A-Z' 'a-z' < file Modern systems, traditional BSD tr '[A-Z]' '[a-z]' < fileOld System V systems
Modern systems allow the use of character classes:
tr '[:upper:]' '[:lower:]' < file
Turn spaces into newlines (ASCII code 012):
tr ' ' '\012' < file
Strip blank lines from file and save in new.file (or use \011
to change successive tabs into one
tab):
tr -s "" "\012" < file > new.file ...
Get Unix in a Nutshell, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.