Use the tr command to easily implement case-insensitive conversion (1)

Source: Internet
Author: User

System Engineers sometimes need to convert the content of a file to uppercase or lowercase during system maintenance. For example, a system engineer downloads a script file from the Internet. However, the owner of the script file has a quirk, and you want to use uppercase letters to describe some remarks. This may be different from the reading habits of some engineers. To this end, you need to convert these uppercase characters into lowercase letters. Of course, System Engineers can manually make modifications one by one. However, the operation is obviously very troublesome. For this reason, the system engineer will wonder if a similar tool is provided in the Unix operating system to convert uppercase letters to lowercase letters? Indeed, a tr command is provided in the Unix operating system. This is a bandit character conversion command, which can implement many functions including case-sensitivity conversion in the Chinese mainland.

1. Basic format requirements for tr character conversion commands.

In Unix operating systems, there are many commands for the entire line of content or fields. In contrast, there are fewer commands to operate on characters in a row. This does not mean that character operations are not important. But because the character conversion command tr can implement most character processing functions. The main function of this command is to replace the string. As shown in, I use the tr command to replace all letters a in a file with letters c. The second example is to convert all lowercase characters in the file to uppercase characters.

The basic format of this command is tr option parameter 1 parameter 2 standard input

Although there are not many options for this command, they are used in comparison. You can use different options to implement different functions. In addition, there is a special part in this command, that is, standard output. What does this mean? In addition, there is no file name parameter in this command. What if I want to convert the content of a file into English characters? Originally, this command only accepts standard input, but does not accept file names as parameters. Therefore, if you want to convert the content of a file to uppercase or lowercase, read the content of the file in the preceding format, then, use tools such as pipeline operators to pass the read content to the command tr. Then use the tr command to convert the case or replace the characters. When using standard input, the system engineer can put the standard input at the end of the command. You can also place the standard input at the beginning of the command using the pipeline operator. I prefer to place the standard input at the beginning of the command. There is no difference between the two methods. It depends on the habits of System Engineers.

Ii. Implement case-insensitive conversion.

The second case mentioned by the pen is case-sensitive conversion of the content in text.txt. Converts all lowercase characters in the file to uppercase. The command used by the author is head-n-2 text.txt | tr '[a-z] ''[A-Z]''. This command does not support file names as its parameters, but supports standard input. For this reason, the author uses the head command to read the first few lines of the file. Because some script files usually place the functional instructions of the script at the beginning of the file, it is reasonable to use the head command to read the first few lines of the text. In this case, you can avoid converting the entire script file. Make sure that the shell is case sensitive. If some commands and parameters are incorrectly converted, an inexplicable error may occur. To this end, you only need to perform case-insensitive conversion on the comments in the first few lines of the script file.

In addition, I used some special metacharacters in this command, such. If metacharacters are encountered in a parameter, escape or reference mechanisms are often used to tell the operating system to treat these characters as common characters. Generally, if System Engineers do not require variable values or command replacement, they must use single quotes or other references or escape characters. According to the mutual protection between double quotation marks and single quotation marks mentioned in the previous article, System Engineers can also assign the expressions in the command to two variables. Then place the variable in double quotation marks to obtain the value of the variable.

3. replace certain characters.

In Unix, there is a special type of file, namely, lst file. This file is very similar to a table file. It uses separators to separate fields. By default, this separator has no fixed limit. For example, you can use the | symbol to separate them, or use a space character or a TAB key to separate them. Even so, readers can set separators based on their own needs. However, this makes file sharing troublesome. Because after a file is created based on a certain separator number, it can only follow this separator number for the next reading. If you want to use other separators, you need to adjust them manually. Now, assume that engineer A in A project team prefers to use the-separator while engineer A prefers to use the | separator. What should we do? Do I need to replace them one by one?

In fact, there are many ways to achieve this conversion. For example, you can use the search command in combination with the loop statement to convert all the-Symbols in this file to the | symbol. However, using cyclic statements and search replacement commands to implement this function is a bit complicated, and every system engineer can write such complex statements in a short time. In fact, using this tr command can easily solve this problem. For example, use tr'-''|. In addition, if the system engineer wants to delete this separator, it is also possible. You can remove all the-delimiters by using option-d.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.