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 convert case on the 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 will explain in detail how to convert case in the Linux command line. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Use tr

The tr (translate) command is one of the easiest commands to use on the command line or script. If you want to make sure you want a string of uppercase strings, you just pass it to tr, as follows:

$echo Hello There | tr [: lower:] [: upper:] HELLO THERE

Here is an example of using this command in a script, when you want to make sure that all text added to the file is in uppercase to maintain consistency. (LCTT note: enter the department name here as an example):

#! / bin/bash echo-n "Enter department name:" read dept echo $dept | tr [: lower:] [: upper:] > depts

Switching the order to [: upper:] [: lower:] has the opposite effect, converting all uppercase department names to lowercase:

Echo $dept | tr [: upper:] [: lower:] > depts

Similarly, you can do the same using the Amurz and Amurz strings of the sed command:

Echo $dept | tr Amurz Amurz > > depts

There is no doubt that reversing the order of the amurz and Amurz strings will have the opposite effect, making the text all lowercase.

Use awk

The awk command allows you to do the same thing using its toupper and tolower options. The commands in the previous script can be replaced in this way:

Echo $dept | awk'{print toupper ($0)}'> > depts

The opposite operation (switching to lowercase) is as follows:

Echo $dept | awk'{print tolower ($0)}'> > depts

Use sed

The sed (stream editor) command can also be used to switch case. It has the same effect as the first of the two commands shown above.

Echo $dept | sed's / [amurz] /\ Ublink _ depts

To switch from uppercase to lowercase, simply replace U near the end of the line with L.

Echo $dept | sed's / [Amurz] /\ Lhammer depts g'> >

Manipulate the text in the file

Both awk and sed can change the text case of an entire file. So, do you find that your boss needs lowercase of all department names? No problem. Simply run the following command with a file name:

$awk'{print tolower ($0)} 'depts finance billing bookkeeping

If you want to overwrite the depts file instead of just displaying it in lowercase, you need to do the following:

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

However, by using sed to make changes, you can avoid the last step, because sed can edit the file "in place", as shown below, the file is complete, but the text is all lowercase:

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

Convert only initials to uppercase

To convert only the first letter of a word in a string to uppercase, do the following:

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

This command ensures that the first letter is capitalized, but does not change the remaining letters.

Make sure only the initials are capitalized.

This is more challenging when you want to change the text so that only the first letter is capitalized. Suppose you are working on a list of staff names and want to format them in a normal "first and last name" way.

1. Use sed

You can use the more complex sed command to ensure the following results:

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

two。 Use Python

If you have installed Python, you can run a command like this, which can also format the text so that each word has only an uppercase letter, and it may be easier to parse than the sed command shown above:

$echo-n "design & engineering" | python3-c "import sys; print (sys.stdin.read (). Title ())" Design & Engineering on "how to convert case on the Linux command line" is here. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

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