Interesting text editing command in linux “tr”
Today we would be talking on the text editing command in linux “tr”
man tr
tr is a powerful text and character editor and manipulator in linux, “tr” which means to translate.
tr translates, substitues or map one set of characters into anothe set.
It is writen as
tr [option] "[SET1]" "[SET2]"
tr takes in information as a standard input and operates on it with a defined sets and passes the result of the operation as a standard output
Options in tr
- -c : Complements the given set. i.e it operates on the characters not in the given set thus complementing your output
- -d : Delete characters in set1 from the standard input
- -s : Squeeze Repetition i.e it identifies repetition in characters from the standard input and squeezes them into one character
Usage of tr
1.Changing all characters from the standard input to uppercase
echo "i am a boy"|tr "[a-z]" "[A-Z]"
We first created our characters using the echo command and pipped “|” it as a standard input into the the “tr” command, we then declared two sets, the first being a set of lowercase characters from a-z and second being our translated set of uppercase characters from A-Z
so “tr” takes in the output of our echo command and checks the characters of the first set to see if there are lowercase characters there, it then translates it to the second set of uppercase characters and passes out the result as standard output there by producing “I AM A BOY”
2. Removing and Deleting of characters
echo "i am a boy"|tr -d "[a]"
Using the same echo output we deleted the character “a” using the “-d” option hereby giving us a standard output from the tr operation as “ i m boy”
3. Squeezeing repetition of characters
echo "ii aamm aa bbooyy"| tr -s "[i,a,m,b,o,y]"
Here we have a statement where all the characters are duplicated, using the “-s” option we would have to squeeze all the duplicated characters into one and produce a readable output “ i am a boy”
4. And lastly we are going to use “tr” command to perfrom an encryption
We can use “tr” to encrypt texts or characters since we would be able to translate a set to anothe set
Lets take for example the ROT13 encryption
ROT13 encryption is just basically a chipher that moves our characters 13 place forward in ASCII
for example we the character “a” moving it 13 place forward would give us the character “n”
so using the “tr” command we can translate “a” to “n”
echo "i am a boy" |tr "[a]" "[n]"
as you can see above all “a” characters where shifted 13 place forward which gave us “n” character
so to encrypt the whole statement we use the command
echo "i am a boy" | tr "[a-z]" "[n-za-m]"
so we shifted all characters in our first set “[a-z]” to the second set n-z then a-m back “[n-za-m]”to have a full rotation of all the alphabets shifted 13 places forward
thus it gave us a standard output of our encrypted stament “v nz n obl”
Incase we have uppercase in our characters we use the command
echo "I am a Boy" | tr "[A-Za-z]" "[N-ZA-Mn-za-m]"
we just added the the uppercase characters as part of our set in the command
“[A-Za-z]” means a set of uppercases A-Z and lowercase a-z, while
“[N-ZA-Mn-za-m]” means a set of uppercases N-Z then A-M back and lowercases n-z then a-m back
So i hope you learnt a lot about the “tr” command and can now implement it in your daily scripting activities, for example creating a simple encryption or decryption cipher script using “tr” command
Thank you.