Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert uppercase and lowercase characters in Linux

Shulou Source: shulou.com Published: 2022-06-01 15:42:28 10月04日 Update

Most people do not understand the knowledge points of this article "how to convert uppercase and lowercase characters in Linux", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to convert uppercase and lowercase characters in Linux" article.

Environment

Centos7

Use tr

If you have a string to make sure it is all uppercase, simply replace it with the tr command:

[root@localhost ~] # echo "Hello World" | tr [: lower:] [: upper:] HELLO WORLD

The following is an example of using this command in a script, and you need to ensure that all text added to the file is uppercase to maintain consistency:

#! / bin/bashread-p "Enter department name:" deptecho $dept | tr [: lower:] [: upper:] > depts

Switching the order to [: upper:] [: lower:] converts all uppercase characters to lowercase:

#! / bin/bashread-p "Enter department name:" deptecho $dept | tr [: upper:] [: lower:] > depts

You can also use "a murz" or "Amurz" to replace case.

#! / bin/bashread-p "Enter department name:" deptecho $dept | tr Amurz Amurz > > depts

The following functions are built into tr:

[: alnum:] all letters and numbers

[: alpha:] all letters

[: blank:] all blanks

[: cntrl:] all control characters

[: digit:] all numbers

[: graph:] all printable characters, excluding spaces

[: lower:] all lowercase characters

[: print:] all printable characters, including spaces

[: punct:] all punctuation marks

[: upper:] all uppercase characters

Use awk

You can use the toupper () and tolower () functions in awk to convert case. The following example is written in the text, converting the input from lowercase to uppercase:

#! / bin/bashread-p "Enter department name:" deptecho $dept | awk'{print toupper ($0)}'> > depts

The following example is written in the text, converting the input uppercase to lowercase:

#! / bin/bashread-p "Enter department name:" deptecho $dept | awk'{print tolower ($0)}'> depts uses sed

You can use the\ U & and\ L & functions in sed to convert case.

Using sed, convert lowercase to uppercase:

#! / bin/bashread-p "Enter department name:" deptecho $dept | sed's / [Amurz] /\ Ubland depts

Using sed, convert uppercase to lowercase:

#! / bin/bashread-p "Enter department name:" deptecho $dept | sed's / [Amurz] /\ Lawash depts above is about "how to convert uppercase and lowercase characters in Linux". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Tags: Characters lowercase content uppercase medium function size text commands letters instances numbers articles knowledge spaces articles input consistency consistency value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn MariaDB Redmi Shulou Information