Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement case conversion in Linux command line

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to achieve case conversion in the Linux command line, the article is very detailed, has a certain reference value, interested friends must read it!

Tr command

Tr (translate) is one of the simplest case conversion commands that can be used on the command line or script. For example, if you want a string of strings to be all uppercase, you can do this with a command like this:

$echo hello alvin | tr [: lower:] [: upper:] HELLO ALVIN

The following is an example of how the command is applied to a script, where all the contents written to the file depts will be in uppercase format:

$echo "Enter department name:" | tr [: lower:] [: upper:] > depts

Changing the order of the above command to [: upper:] [: lower:] will get the opposite result, and all entered letters will be lowercase:

$echo "Enter department name:" | tr [: upper:] [: lower:] > depts

You can use Amurz Amurz instead of [: upper:] [: lower:] to achieve the same effect:

$echo "Enter department name:" | tr Amurz Amurz > > depts

Awk command

The awk command performs case conversion with the options toupper and tolower. The case conversion of the script described above can be done with this command as follows:

$echo "Enter department name:" | awk'{print toupper ($0)}'> > depts

The above is converted to uppercase letters, and the following is how to convert to lowercase letters:

$echo "Enter department name:" | awk'{print tolower ($0)}'> > depts

Sed command

The sed (stream editor) command also does a good job of case conversion, and the following command can achieve the same effect as the first two commands:

$echo "Enter department name:" | sed's / [a murz] /\ Ublink Universe g'> depts

To do the reverse conversion, simply replace U in the above command with L and [a murz] with [Amurz]:

$echo "Enter department name:" | sed's / [Amurz] /\ Lawyer _ depts

Modify the case format of the text content in the file

Both the awk and sed commands can convert the entire document content to lowercase, so if you need to convert the entire document content to lowercase, you can output the contents of a file to the screen in lowercase with the following command:

$awk'{print tolower ($0)} 'depts

The results are as follows:

Enter department name: finance billing bookkeeping

Note that this result only converts what is printed on the screen to lowercase, and the uppercase letters in your own document remain unchanged, as you can see by using the cat depts command. If you want to change the case of the document exactly, you can do it with the following command:

$awk'{print tolower ($0)} 'depts > depts- $mv depts- depts

The sed command can perform the same function as awk, but with a slight difference in usage, it is used as follows:

$sed's / [Amurz] /\ Lancaplash g'depts

Capitalize only the initials

Capitalize only the first letter of each word in a string, which can be done with the following command:

$echo design\ & engineering | sed-e "s /\ b\ (.\) /\ u\ 1zag" Design & Engineering

The above command allows only the first letter to be capitalized and the other letters unchanged.

Make sure only the initials are capitalized.

When you need to do this with a large number of text, such as displaying a large number of people's names in the correct format, what you need to do becomes a little more difficult. Here are two ways to accomplish this task:

Complete with the sed command

To ensure that the results are correct, a more complex sed command format is required:

$echo design\ & ENGINEERING | sed's /\ b\ ([[: alpha:]]\)\ ([[: alpha:]] *\)\ b /\ u\ 1\ L\ 2Universe g'Design & Engineering

Do it with python code

If you have python installed on your Linux, you can capitalize the text with the following command, and the python code is much easier to understand than the regular formula of the sed command above:

$echo-n "design & engineering" | python3-c "import sys; print (sys.stdin.read (). Title ())" Design & Engineering is all the contents of the article "how to convert case in the Linux command line". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report