You can think of tr as a (very) simplified variant of sed: it will replace one character with another or remove some characters altogether. You can also use it to remove repeated characters. And that's about all tr can do.
So, why use it instead of sed? To simplify your life, of course. For example, if we wanted to replace all occurrences of the letter "a" with the letter "z," we can use tr a z, which is undoubtedly simpler than sed -e s/a/z/g, especially when it is used in a script, where quote-escaping can give us a lot of headaches. Also, when we use tr, we can avoid writing those pesky regular expressions.
Using tr is simple: to replace all occurrences of one character with another, use the notation shown in the previous paragraph. When you need to replace more than one character, use something like tr abc xyz
, which replaces all occurrences of the letter "a" with the letter "x," all letters "b" with the letter "y," and all letters "c" with the letter "z." The number of characters listed in these two groups does not have to be equal.
You can also specify ranges of characters. For example, tr a-z A-Z will replace all lower case letters with their upper case equivalents (for example, it will turn "no smoking" into "NO SMOKING"). This particular trick comes in handy when you want to emphasize a section of the text you are editing in the vi editor. Simply hit the Escape key, press :, type 2,4!tr 'a-z' 'A-Z', and hit the Return key. Lines 2 through 4 will now turn into upper case.
Another time you will find tr very helpful will be when somebody sends you a text file created on a Mac OS or DOS/Windows machine. When not saved using the UNIX newline line ends, such files need to be converted into the native UNIX format or else some command utilities will not process them correctly. Mac OS ends lines with the carriage return character, and many text processing tools treat such files as a single line. To fix that, we can use the following tricks:
- Mac -> UNIX:
tr '\r' '\n' < macfile > unixfile - UNIX -> Mac:
tr '\n' '\r' < unixfile > macfile
Microsoft DOS/Windows convention is to end each line of text with the carriage return character followed by the newline character. To fix that, use the following commands:
- DOS -> UNIX:
tr -d '\r' < dosfile > unixfile - UNIX -> DOS: in this case we need to use
awk, because tr cannot insert two characters in place of one. The command to use isawk '{ print $0"\r" }' < unixfile > dosfile
Another time you will need to use tr is when you need to do some simple cleaning up of a text file, like removing tabs with tr -d '\t', removing extra spaces with tr -s ' ', or joining separate lines into one with tr -d '\n'. Again, all these commands can be used inside vi; just remember to precede them with the range of lines you wish to process and the exclamation mark (!), as in 1,$!tr -d '\t' (the dollar sign represents the last line).
Questions or comments? I'd love to hear from you -- send mail to jacek@artymiak.com.
Next time, we'll take a look at uniq. See you
then!
- Read the other tips in this series:
- Get to know your textutils
- Concatenating files with cat
- Reading text streams in chunks with head and tail
- Sorting files with sort and tsort
- The tr man page is at gnu.org. You'll find even more info on these useful tools in the GNU text utilities manual (an expanded view of the same TOC lives at MIT, where you can also find a great list of even more useful GNU tools).
-
Windows users can find these tools in the Cygwin package.
-
Mac OS X users may want to try Fink, which installs a rich UNIX environment under the new Mac OS X.
-
Something just not working for you? Try checking the Frequently asked questions for GNU textutils.
-
Need more introductory info before delving in to the tools we've covered here? Try starting with UNIXhelp for users.
-
Of course, the classic work in this field is Unix Power Tools, from O'Reilly and Associates (Jerry Peek, Tim O'Reilly and Mike Loukides 1997; ISBN 1-56592-260-3).
-
Enchanted with UNIX text utilities (and with UNIX as such)? Then you'll love the classic article by Thomas Scoville, "UNIX as Literature".
-
If you need a tool that does something not covered by these basic tools -- say, a command-line interface to search the Web -- simply browse through some of the sites that list such tools. A Google search on
linux plus text plus utilitiesyields many such collections -- for instance this one from Linux.org. Of course, sources like Freshmeat or SourceForge will list zillions of these custom utilities as well. -
You might also want to check out IBM's Tools and Toys for UNIX page.
-
And if you still can't find it, just learn to write your own UNIX utilities with the developerWorks series "Unix utilities", Parts 1 through 4. The series includes "Yet another new component architecture," "Simple tools, complex problems," "The easiest component you'll ever write," and culminates with "Unix's lessons for component architectures."
-
Once you've learned to write your own utilities, learn how to turn them into a library! It's easy with the developerWorks tutorial "Building a cross-platform C library".
- Learn to use the vi editor in this developerWorks tutorial "vi intro -- the cheat sheet method".
- Find the Linux resource you're looking for in the developerWorks Linux zone.
Jacek Artymiak works as a freelance consultant, developer, and writer. Since 1991 he's been developing software for many commercial and free variants of UNIX and BSD operating systems (AIX, HP-UX, IRIX, Solaris, Linux, FreeBSD, NetBSD, OpenBSD, and others), as well as MS-DOS, Microsoft Windows, Mac OS, and Mac OS X. Jacek specializes in business and financial application development, Web design, network security, computer graphics, animation, and multimedia. He's a prolific writer on technology subjects and the coauthor of "Install, Configure, and Customize Slackware Linux" (Prima Tech, 2000) and "StarOffice for Linux Bible" (IDG Books, 2000). Many of Jacek's software projects can be found at SourceForge. You can learn more about him at his personal Web site and contact him at jacek@artymiak.com.
Comments (Undergoing maintenance)





