Changing with tr

Sometimes you just have to make changes to a file to change all occurrences of one term or character to another. For example, you might have reversed the case in a file (by accidentally typing with Caps Lock on...argh!) and need to change it back. Or you might want to turn a document into a list of words (one per line) that you can sort or count. The tr utility is just what you need (see Code Listing 6.20).

Code Listing 6.20. Use tr to translate characters in files.
[jdoe@frazz jdoe]$ cat limerick
There once was a man from Nantucket,
Who carried his lunch in a bucket,
Said he with a sigh,
As he ate a whole pie,
If I just had a donut I'd dunk it.

[jdoe@frazz jdoe]$ cat limerick | tra-zA-Z A-Za-z tHERE ONCE WAS A MAN FROM nANTUCKET, ...

Get Unix Third Edition: Visual Quickstart Guide 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.