Using the Linux “Tr” Command
The tr function can be used to carry out tasks including getting rid of redundant characters, changing the capital letters to lower letters, and replacing and getting rid of simple character. It is frequently combined with other commands via piping.
In this section, we utilize the Linux “tr” command to replace the characters. Let’s begin putting the command into action on Linux. First, we open the terminal. Then, we use the “echo” command to accomplish this. To display the lines of text or characters that are passed as command-line parameters, use the echo function. The most frequently used function in the shell scripts on Linux is this one. We start with the “echo” keyword then type the statement that we want to use inside the inverted comma which is “you are the best” followed by the bar “|”, the “tr” keyword, the letter that we want to replace, “e,” and the letter “s” which is the character that appears in that location where “e” is used in the echo sentence.
When we run this command, the terminal window displays the output which is the echo statement where the character “e” is replaced with the character “s”. The result is “you ars the bsst”.
Converting the Lowercase Characters to Uppercase Characters
In this section, we’ll show you how to change the lowercase letters into uppercase letters using one of two methods: either we may provide the character range or we can specify the interpreted sequences to change the characters. The lowercase characters go in the [:lower] sequence, while the uppercase characters go in the [:upper:] sequence. Now that the command is created, it is put into action using the “echo” statement first and then changing the lower characters to upper characters. The fruit names that are included in the echo statement are “Apple”, “Mango”, “Banana”, and “Grapes”.
As you can see, the first character in each of these elements is uppercase, while the remaining characters are lowercase. To change the remaining characters to uppercase, we use the “tr” command in which we specify the character range as “[a-z]” and “[A-Z]” where the first specifies the range of the alphabet using the lower characters, and the second specifies it using the upper characters. This essentially indicates that all lowercase characters from “a” to “z” in the echo statement are changed to uppercase.
Now that the command is executed, you can see that the lowercase characters are changed to uppercase characters in the following output:
APPLE MANGO BANANA GRAPES
Now, in the following section, we’ll utilize a different technique to change the lower case to upper case using the “tr” command with the “[:lower]” and “[:upper:]” terms. To accomplish this, we use the same echo statement and then type “tr” followed by the “[:lower:]” and “[:upper:]” keywords. Using “lower” first and then “upper” means that all of the lowercase letters in the echo statement are changed to uppercase.
When we execute this command, it produces the same results as the previous one:
Removing Specific Characters
In this section, we’ll use the “-d” option of the “tr” command to remove a specific character from the echo statement. Using a specific character in the “tr” command with the “-d” parameter, we can delete that character from the line or the file.
Let’s remove the character using the command on the terminal. First, we use the “My name is Alex” echo statement followed by the bar “|”. After which, we write “tr” followed by the “-d” flag to delete the character. Finally, we provide the character that we want to remove from the statement which is “e” in the inverted comma.
When we run this command, the “e” character is removed from the line and the text is changed to “My name is Alx”.
Deleting Digits
Using the “tr” command, “-d” option, and the “[:digit:]” expression, we may additionally delete all the digits in a line or file. The word “digit” must be enclosed in square brackets and a colon. Let’s begin using the “Alex got 98% marks” echo statement followed by the “|” bar, “tr”, the “-d” option, and the “[:digit:]” keyword. This deletes all the digits that are present in the echo statement since there are two digits in the “98” echo statement which means that both of these digits are removed from the line when we run this command:
Following the execution of this command, the “Alex got% marks” echo statement is displayed in the output. As you can see, both digits are deleted from the line, keeping only the characters and the “%” symbol that we used in the line.
Eliminating Newline Characters
In this section, we remove the file’s newline character. On the desktop, there is a file called “file.txt” that holds some information. First, we use the cat command to open the file on the terminal. To use this command, type “cat” followed by the file’s name, “file.txt.” The file opens on the terminal when we execute this command:
When the command is executed, a file which contains several names is opened. Each name is written on a separate line. Now, we display the entire name on a single line by deleting the newline character.
Jhon
Watson
David
We type the following command on the terminal. First, we type “cat”. Then, we use the “file.txt” file name. Then, we use bar “|”. After that, we type the “tr” command. Then, we use the “-s” option which is used to convert the newline characters into spaces. Lastly, the “\n” inside of the inverted commas is used. This converts the newline characters into spaces and displays all of the lines in a single line.
The output of the command is “Alex”, “Jhon”, “Watson”, and “David”. When the command is performed, it prints the file’s lines on a single line which is separated by spaces. The newline characters are deleted and changed into spaces.
Conclusion
This article looked at the “tr” command in Linux which can be used for a variety of tasks. The “tr” command can be used with a variety of flags such as “-s”, “-d”, and others. In the aforementioned article, we utilized the numerous instances of the “tr” command in which we substituted the characters, deleted the characters, removed the digits, and also removed the newline characters from the files and changed them to spaces before displaying the entire text of the lines in a single line.