shapeshed

Ruby & JavaScript Hacker

Nifty Unix Tools - Tr

tr - a simple translation tool

I’m an unashamed command line kid old git. For getting things done quickly and efficiently there are a wealth of tools at your fingertips. tr is just one of those tools. It lets you quickly and simply translate characters in a string.

It doesn’t have the power of other tools like sed or awk but for basic string manipulation it is pretty good. One example would be that we want to change uppercase text to lower case

1
2
echo 'HI THERE' |  tr '[:upper:]' '[:lower:]'
hi there

Or we might have a comma separated list that we want to strip the commas out of and to replace them with a new line

1
cat file.csv | tr ',' '\n' > new.txt

This translates each comma to a new line so ‘dog, cat, horse’ becomes

dog
cat
horse

Deleting characters

tr also lets you delete characters. So if you need to quickly clean up a string you can do:

1
2
echo 'clean$ me$ $up$ please$' | tr -d '$'
clean me up please

Because Unix lets you pipe the output we can write this to a file easily

1
echo 'clean$ me$ $up$ please$' | tr -d '$' > file.txt

We could also read in a big file, pipe it to the tr command and then send the output to another file

1
cat big_file.txt | tr -d '$' > clean_file.txt

More complex uses

You can pipe replacements together for more complex translations. Let’s say someone sends you a file with spaces in it. You want to remove the spaces and whilst you are at it make the filename lowercase.

1
mv Crap\ File\ nAme.txt `echo Crap\ File\ nAme.txt | tr -d '\' | tr ' ' '_' | tr '[:upper:]' '[:lower:]'`

This will rename ‘Crap File nAme.txt’ to ‘crap_file_name.txt’

These little commands are really useful. If you find yourself doing the same thing over and over again you can save it to an executable script. I regularly get clients sending me documents for the web with spaces in. So I use this script to quickly lowercase the filenames and clean up. This lets me run clean_filenames.sh in any directory and the filenames will be cleaned up. If you put your scripts in your bin folder and make them executable they’ll be available via tab completion too. So you can just type ‘clean_’ hit tab, then return and you are done.

tr is a simple tool that can’t compete with the power of sed or awk but there are times when using a more heavyweight is unnecessary.

For more run ‘man tr’ from the command line.