Introduction
Linux and Unix are both widely used operating systems. These operating systems are known for the use of command-line interfaces. Everyone knows the significance of commands in a command line interface. The “tr” or transfer character command is one such important command in the Linux and Unix systems.
In this article, we will discuss the tr command in Linux in detail. Furthermore, you will get the basic idea behind every option and set with appropriate examples. So, let’s start reading about this useful command.
Essentials for using the “tr” command
The prerequisites or essentials for using the “tr” command are limited but cannot be ignored. You need a basic understanding of character sets. The Linux tr command is a text-processing tool that enhances the interface’s capabilities. It helps transform text data, which is composed of a character set. The next thing you need is proficiency in command-line tools. Along with these two specifications, it is important to have access to Unix and Linux systems to implement the transfer character command. All these essentials ensure proper usage of the tr command in Linux to perform a variety of tasks.
Understand the options and sets for the “tr” command
Options and sets are essential parts of a command syntax for certain commands like transfer characters. These elements modify the behavior of the command. As far as the options of the “tr” command are concerned, they are preceded by a hyphen (-) or a double hyphen (–).
On the other hand, some sets include character, individual, and predefined range. A few examples of sets could be “\a,” which produces a sound when encountered. The other one could be “\f”, which moves the cursor one position backward. We will now move forward to the next section about the transfer command.
Basic tr Command Syntax in Linux
As discussed above, the tr command in Linux is an excellent tool for translating or deleting characters. We have inserted a basic syntax to guide how a Linux command like tr looks.
Let’s discuss each component of the above syntax one by one.
- tr: This is the transfer character command itself.
- [options]: This is used to modify the command’s behavior. The following elements can replace the term “options.”
- -d: Deletes the character set.
- -s: Squeezes the character set.
- -c: Complements or replaces any character not found in SET1.
- -t: This option truncates or shortens the character SET.
- SET1: This is the set of characters that is to be translated or deleted.
- SET2: This is the replacement set of characters. It will take place on SET1 as per the instructions.
Linux tr Command Examples
This section is about testing the syntax that we have learned. We will go through Linux tr command examples to understand the transfer character utility in depth.
Through the command below, we will translate all lowercase letters of the word “welcome” to their uppercase equals.
We will now move on to some other important examples that will show what a tr command does in Linux.
Delete a particular character
The command will remove “c” from the input string “Welcome” The—d option is used to delete characters specified in SET1.
Translate the characters to uppercase
Here, the command will translate the characters of the entire string. The character in SET 1, i.e., “welcome” will be translated to “WELCOME” So, all lowercase letters are translated to uppercase, and digits are translated to lowercase.
Squeeze repeated character
The repeated insertion of the letter “e” will be cut to one through the above command. The -s option ensures the squeezing of the repeated characters. So, “Welcomee” becomes “Welcome”.
Complement character
The command asks the system to replace the “welcome” with “*.” So, only the letters “w” will be on display. Take a look at the output.
Also Read: Linux Commands Cheat Sheet: Advanced Techniques
Summing Up
The Linux tr command is an adaptable tool for text processing. It allows users to translate or delete characters based on selected sets and options. The prerequisites for using the tr command in Linux include understanding the essentials of character sets and proficiency in command-line tools. This ensures effective utilization of the transfer character command. The command’s syntax comprises options that modify its behavior, such as deleting characters, squeezing repeated characters, complementing characters, and truncating sets.
We have inserted certain examples to let you explore how the tr command can be used to perform various text manipulation tasks, from translating lowercase to uppercase letters to deleting specific characters and squeezing repeated characters. We hope this piece has led to a better understanding of the transfer command, CLI, Linux, and Unix as a whole.