- [Instructor] Hi and welcome to the fifth video
of section two, translating with tr.
In the previous video, we've learned
how to play with x arcs.
Let's move on to the topic.
In this video, we'll see how to use tr
to perform basic translation between sets.
Tr is a small and beautiful command
in the Unix command warrior toolkit.
It is one of the important commands,
frequently used to craft beautiful one liner commands.
It can be used to perform substitution of characters,
deletion of characters and squeezing
of repeated characters from the standard input.
It is often called translate,
since it can translate a set of characters to another set.
Tr accepts input only from STD in, standard input,
and cannot accept input through command line arguments.
It has the following invocation format.
Input characters from STD in are mapped
from set one to set two and the output
is written to STD out, standard out.
Set one and set two are character classes
or sets of characters.
If the length of the set is unequal,
set two is extended to the length of set one
by repeating the last character
or else if the length of set two is greater than that
of set one, all the characters
exceeding the length of set one are ignored from set two.
Let's see how to do it.
To perform translation of characters in the input
from uppercase to lowercase, use the following command.
A-Z and lowercase a, lowercase z are the sets.
We can specify custom sets as needed
by appending characters or character classes.
ABD-{, a-c-x, a-c 0-9 and so on are valid sets.
We can define sets easily.
Instead of writing continuous character sequences,
we can use the start char, end char format.
It can also be combined with any other characters
or character classes.
If start char end char is not a valid continuous character
sequence, they are then taken as a set of three characters,
for example start char, minus and end char.
You can also use special characters
such as /t, /n or any asker characters.
By using tr with the concept of sets,
we can map characters from one set to another easily.
Let's go through an example how to use tr
for encrypting and decrypting numeric characters.
Let's look at another interesting example.
Rot13 is a well known encryption algorithm.
In the rot13 scheme, the same function
is used to encrypt and decrypt text.
The rot13 scheme performs basic alphabet rotation
of characters for 13 characters.
Let's perform rot13 using tr as follows.
The output will be
by sending encrypted text again
to the same rot13 function we get
the output will be
tr can be used to convert tab characters
into spaces as follows.
We saw some basic translations using the tr command.
Let's see what else tr can help us achieve.
Deleting characters using tr.
Tr has an option, minus d, to delete a set
of characters that appear in STD in by using
the specified set of characters to be deleted as follows.
For example,
Complementing character set, we can use a set
to complement set one by using the minus c flag.
Set two is optional in the following command.
The complement of set one means that it is in the set
having all the characters except the characters in set one.
The best usage example is to delete all the characters
from the input text except the ones
specified in the complement set.
For example,
here the complement set is the set to contain all numerals,
space characters and new line characters.
All other characters are removed
since minus d is used with tr.
Squeezing characters with tr.
The tr command is very helpful
in many text processing contexts.
Repeating continuous characters should be squeezed
to a single character in many circumstances.
Squeezing of white space is a frequently occurring task.
Tr provides the minus s option
to squeeze repeating characters from the input.
It can be performed as follows.
Let's use tr in a tricky way to add a given list
of numbers from our file as follows.
How does this hack work?
Here the tr command is used to replace /n
with the plus character, hence we form the string 1+2+3+..5+
but at the end of the string,
we have an extra plus operator.
In order to nullify the effect
of the plus operator, zero is appended.
$ operation performs a numeric operation,
hence it forms a string as follows.
If we use a loop to perform the addition
by reading numbers from a file,
it would take a few lines of code.
Here's a one liner that does the trick.
Tr can also be used in this way
to get rid of extra new lines as follows.
In the proceeding usage of tr, it removes the extra
/n characters into a single /n, new line character.
Character classes,
tr can use different character classes as sets.
The different classes are as follows,
alnum, alphanumeric characters,
alpha, alphabetic characters,
cntrl, control non-printing characters,
digit, numeric characters, graph, graphic characters,
lower, lowercase alphabetic characters,
print, printable characters, punct, punctuation characters,
space, white space characters, upper, uppercase characters,
x digit, hexidecimal characters.
We can select the required classes and use them as follows.
Awesome, in this video we have successfully
learned about translating with tr.
Hope you enjoyed watching this video.
In the next video, we'll see
about checksum and verification.