tr command is used as a character-based search and replace function.
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
$ 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
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?
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
-d option, we can delete specific characters from a string.
$ echo 'Removing all vowels' | tr -d 'aeiou' Rmvng ll vwls
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
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.
The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.$ Check price
Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.$ Check price