07. Transliterating text tr

The tr command is used as a character-based search and replace function.

Simple Bioinformatics

If you're a bioinformaticist, you may find the need to replace all T characters from a DNA strand to U's to obtain the corresponding mRNA strand. We can do this in a one-liner with the tr command.

$ echo 'CATCGTAGCTAGTCACTG' | tr T U
CAUCGUAGCUAGUCACUG

In biology, each DNA strand has a corresponding strand. G's pair to C's and T's pair to A's. Can we use tr to find the corresponding strand?

$ echo 'CAGTCGTACGTACGT' | tr 'ATGC' 'TACG'
GTCAGCATGCATGCA

Lowercase to Uppercase

You can use this function to map lowercase letters to uppercase.

$ echo 'what is up world?' | tr a-z A-Z
WHAT IS UP WORLD?

From standard in and to standard out

You may also use redirection to direct an input file and write to some output.

$ cat fox.txt
The quick brown fox jumped over the lazy dog.
$ tr 'aeiou' 'AEIOU' < fox.txt
ThE qUIck brOwn fOx jUmpEd OvEr thE lAzy dOg.
$ tr 'aeiou' 'AEIOU' < fox.txt > vowelsCappedFox.txt
# Output to vowelsCappedFox.txt

Deleting specific characters

With the -d option, we can delete specific characters from a string.

$ echo 'Removing all vowels' | tr -d 'aeiou'
Rmvng ll vwls

Squeezing text

We can eliminate repeated instances of a letter with the squeeze option, -s. This removes all repeated characters that occur in sequence.

$ echo '1111112222233333344444' | tr -s 123
12344444

Generating a secret message

We can generate an amateur secret message by mapping our lowercase characters to their inverse correspondence.

$ cat fox.txt | tr a-z zyxwvutsrqponmlkjihgfedcba
Tsv jfrxp yildm ulc qfnkvw levi gsv ozab wlt.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Ad