Reading guide | When editing text, case is often the place to pay attention to , Case conversion is a tedious and tedious job , Fortunately ,Linux There are a lot of things that can make this job easy command . Next, let's look at some of the cases that are converted command . |
tr command
tr (translate) It can be used on the command line or Script One of the simplest case conversion commands on . For example, if you want a string to be all uppercase , You can do this with the command shown below :
$ echo hello alvin | tr [:lower:] [:upper:]
HELLO ALVIN
The following is how the command is applied to Script Examples , write file depts The content of the plenary session is in capital form :
$ echo "Enter department name: " | tr [:lower:] [:upper:] >> depts
The order of the above commands is changed to [:upper:] [:lower:] It's going to get the opposite result , All letters entered will be lowercase :
$ echo "Enter department name: " | tr [:upper:] [:lower:] >> depts
You can use it. A-Z a-z Instead of [:upper:] [:lower:] To achieve the same effect :
$ echo "Enter department name: " | tr a-z A-Z >> depts
awk command
awk Order to use toupper and tolower These two options complete the case conversion . The case conversion of the script described above can be done with this command in the following way :
$ echo "Enter department name: " | awk '{print toupper($0)}' >> depts
The above is converted to capital letters , Here's how to convert to lower case letters :
$ echo "Enter department name: " | awk '{print tolower($0)}' >> depts
sed command
sed (stream editor) The command can also do the case conversion well , The following command can also achieve the effect of the previous two commands :
$ echo "Enter department name: " | sed 's/[a-z]/\U&/g' >> depts
To complete the reverse conversion , Just put the U Switch to L 、[a-z] Switch to [A-Z] that will do :
$ echo "Enter department name: " | sed 's/[A-Z]/\L&/g' >> depts
Change the case of the text in the file
awk and sed Command can complete the case conversion of the entire document content , So if you need to convert the entire document to lowercase , You can use the following command to output the contents of a file in lowercase to the screen :
$ awk '{print tolower($0)}' depts
The result is as follows :
enter department name:
finance
billing
bookkeeping
Note that this result only converts what is printed on the screen to lowercase , There is no change in capital letters in the document itself , Use cat depts Command to see . If you want to really change the case of the document content , You can do this with the following command :
$ awk '{print tolower($0)}' depts > depts-
$ mv depts- depts
sed Commands can be done and awk Same function , It's just a little different in usage , Its usage is as follows :
$ sed 's/[A-Z]/\L&/g' depts
Just capitalize the first letter
Capitalize only the first letter of each word in a string , This can be done with the following command :
$ echo design \& engineering| sed -e "s/\b\(.\)/\u\1/g"
Design & Engineering
The above command can only capitalize the first letter and keep the rest of the letters unchanged .
Make sure that only the initials are capitalized
When you need to do this with a lot of text , For example, to display the names of a large number of people in the correct format , What needs to be done will be a little harder . Here are two ways to do this :
use sed Command complete
To make sure the results are correct , It needs to be more complicated sed Command format :
$ echo design \& ENGINEERING | sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g'
Design & Engineering
use python Code completion
If your Linux Installed on python , You can use the following command to complete the capitalization of the text ,python The code is better than above sed The regular form of a command is much easier to understand :
$ echo -n "design & engineering" | python3 -c "import sys; print(sys.stdin.read().title())"
Design & Engineering
Linux There are many commands for case conversion , Which one works better depends on whether you're dealing with a string of characters or an entire file . I hope the various commands in this article can help you , If you have any questions about these orders , Feel free to leave a comment !Linux It's time to learn