Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert uppercase and lowercase characters on the Linux command line

Shulou Source: shulou.com Published: 2022-06-03 15:38:38 09月20日 Update

This article mainly introduces "Linux command line how to convert case characters to content". In daily operation, I believe many people have doubts about how to convert case characters to content on Linux command line. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to answer your doubts about "Linux command line how to convert case characters to content"! Next, please follow the small series to learn together!

If you have a string that you want to make sure is all caps, simply replace it with tr:

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

Here is an example of using this command in, making sure that all text added to the file is capitalized for consistency:

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

Switching the order to [:upper:] [:lower:] converts all upper-case characters to lower-case:

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

You can also use "a-z" "A-Z" to replace case.

#!/ bin/bashread -p "Enter department name: " deptecho $dept | tr a-z A-Z>> 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 upper-case characters

use awk

In awk, you can use the toupper() and tolower() functions to convert case.

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

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

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

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

using the sed

The\U& and\L& functions can be used in sed to convert case.

Use sed to convert lowercase to uppercase:

#!/ bin/bashread -p "Enter department name: " deptecho $dept | sed 's/[a-z]/\U&/g' >> depts

Use sed to convert uppercase to lowercase:

#!/ bin/bashread -p "Enter department name: " deptecho $dept | sed 's/[A-Z]/\L&/g' >> depts

summary

There are many ways to replace upper and lower case letters in linux, and you can choose a command you can remember to use.

At this point, the study of "how to convert case characters from Linux command line" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Characters size uppercase commands lowercase internal content learning functions letters text instances numbers more spaces help input utility consistency next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn NVidia Apple OPPO Reno