A Little Devil Called tr
The program called tr is not a big program; it is quite small and not extremely powerful. However, if you write scripts, you will treasure it as one of your favorites. It is a typical script program, reading from stdin and writing to stdout; there are no file names to provide as arguments. The main function is translating characters. A second important function is deleting characters. Furthermore, tr is capable of squeezing repeated characters into one, but that particular function is rarely used.
Let us begin with translating characters. The tr command takes the form:
tr
While tr reads its input, it replaces characters appearing in string1 by the corresponding characters in string2. So, the command tr abc def will replace a line like “the quick brown fox quickly jumped over the lazy dog” into “the quifk erown fox quifkly jumped over the ldzy dog”. Well, that doesn't make sense, but it does demonstrate how tr works.
Have you ever wanted to capitalize or de-capitalize a file? To capitalize it, you can use:
tr abcdefghijklmnopqrstuvwxyz \
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Luckily, we can also use ranges of characters to specify the characters more efficiently:
tr a-z A-ZEver had those horrible upper case DOS file names? Here's a Bourne script to take care of them:
for f in *; do
mv $f `echo $f | tr A-Z a-z`
done
Many UNIX editors allow some text to be processed by the shell. For
example, to replace all upper case characters of the next paragraph
with lower case while in vi, type:
!}tr A-Z a-zAs another example, the command:
!jtr a-z A-Zcapitalizes the current and next line (the character after the ! is a movement character).
If you read the International Obfuscated C Code Contest (ftp://ftp.uu.net./pub/ioccc/), you frequently see that part of the hints are coded by a method called rot13. rot13 is a Caesar cypher, i.e., a cypher in which all letters are shifted some number of places. For example, a becomes b, b becomes c, ..., y becomes z, and z becomes a. In rot13 each letter is shifted 13 places. It is a weak cypher, and to decipher it, you can use rot13 again. You can also use tr to read the text in this way:
tr a-zA-Z n-za-mN-ZA-M
Another interesting way to use tr is to change files from Macintosh format to UNIX format. For returns, the Macintosh uses \r while UNIX uses \n. GNU tr allows you to use the C special characters, so type:
tr \r \nIf you don't have GNU's version of tr, you can always use the corresponding octal numbers as shown here:
tr \015 \012You might wonder what would happen if the second string is shorter than the first string. POSIX says this is not allowed. System V says that only that portion of the first string is used that has a matching character in the second string. BSD and GNU pad the second string with its final character in order to match the length of the first string.
The reason this last method is handy becomes clearer when we take complements into account. Assume you wish to make a list of all words and keywords in your listing. When you use -c, tr complements the first string. In C, all identifiers and keywords consist of a-zA-Z0-9_, so those are the characters we want to keep. Thus, we can do the following:
tr -c a-zA-Z0-9_ \n
If we pipe the tr output through sort -u, we get our desired list. If we follow POSIX, the second string would have to describe 193 newline characters (described as \n*193 or \n*). If we use system V, only the zero byte is translated to a newline, since the complement of a-zA-Z0-9_ starts with the zero byte.
The second important use of tr is to remove characters. For this option, you use the flag -d with one string as an argument. To fix up those nasty MS-DOS text files with a ^M at the end of the line and a trailing ^Z, specify tr in this way:
tr -d \015\032
Many people have written a program in C to do this same operation. Well, a C program isn't necessary—you only need to know the right program, tr, with the right flags. The -d flag isn't used often, but is nice to have when needed. You can combine it with the -c flag to delete everything except characters from the string you supplied as an argument.
Repeated characters can be squeezed into a single one using the -s option with one string as an argument. It can also be used to squeeze white space. To remove empty lines, type:
tr -s \n
The -s option can be used with two strings as arguments. In that case, tr first translates the text as if -s were not given and then tries to squeeze the characters in the second string. For instance, we can squeeze all standard white space to a single space by specifying:
tr -s \n [ *]The -d flag can also be used with two strings: the characters in the first string will be removed and the characters in the second string will be squeezed.
tr may not be a great program; however, it gets the job done. It is particularly useful in scripts using pipes and command substitutions (i.e., inside the back quotes). If you use tr often, you'll learn to appreciate its capabilities. Small is beautiful.
Hans de Vreught (J.P.M.deVreught@cs.tudelft.nl) is a computer science researcher at Delft University of Technology. He has been using UNIX since 1982 (Linux since 0.99.13). He likes non-virtual Belgian beer, and he is a real globetrotter, having already traveled twice around the world.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
| Android's Limits | Jun 04, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Introduction to MapReduce with Hadoop on Linux
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Weechat, Irssi's Little Brother
- One Tail Just Isn't Enough
- Android's Limits
- http://www.pldhs.com/
8 min 23 sec ago - Free is costly
1 hour 23 min ago - Bought photoshop CS5 for developing a website :(
1 hour 40 min ago - Reply to comment | Linux Journal
2 hours 28 min ago - Reply to comment | Linux Journal
2 hours 28 min ago - Replica Watches
4 hours 53 min ago - Reply to comment | Linux Journal
9 hours 4 min ago - on the path to understanding
9 hours 7 min ago - As a fisher,we know that a
1 day 4 hours ago - All I Say Is Worth Share!
1 day 5 hours ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Filtering fields of a file
Here r my set of records in file
File.txt:
1::2:3:4
4:5:6:7:8
7:8:9:10:
7: 8 9:1:2:3
::::
The fields are separated by field separator(:).
I want to print the fields of this record. There are two problems while printing this. 1) some fields are blank(::), 2) Some fields contains more then one characters separated by space character (:8 9:)
Can it be done using tr ?
pleas let me wat it does
tr "(){}[]\"'<>;&@" " " < $data_dir/SCell_$dt.wk > $data_dir/SCell_$dt.sort.init
Re: Take Command: A Little Devil Called tr
I would like to be able to count how many of each letter of the alphabet are in a file, reguardless of case. I'm trying to figure out how to make tr replace EACH letter with the letter followed by a line feed.
Thank you.