translated

This commit is contained in:
geekpi 2020-03-10 08:29:09 +08:00
parent 062fe204c1
commit f812373394
2 changed files with 161 additions and 161 deletions

View File

@ -1,161 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Converting between uppercase and lowercase on the Linux command line)
[#]: via: (https://www.networkworld.com/article/3529409/converting-between-uppercase-and-lowercase-on-the-linux-command-line.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
Converting between uppercase and lowercase on the Linux command line
======
Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy.
[andy.brandon50][1] [(CC BY-SA 2.0)][2]
There are many ways to change text on the Linux command line from lowercase to uppercase and vice versa. In fact, you have an impressive set of commands to choose from. This post examines some of the best commands for the job and how you can get them to do just what you want.
### Using tr
The **tr** (translate) command is one of the easiest to use on the command line or within a script. If you have a string that you want to be sure is in uppercase, you just pass it through a **tr** command like this:
```
$ echo Hello There | tr [:lower:] [:upper:]
HELLO THERE
```
[[Get regularly scheduled insights by signing up for Network World newsletters.]][3]
Below is an example of using this kind of command in a script when you want to be sure that all of the text that is added to a file is in uppercase for consistency:
```
#!/bin/bash
echo -n "Enter department name: "
read dept
echo $dept | tr [:lower:] [:upper:] >> depts
```
Switching the order to [:upper:] [:lower:] would have the opposite effect, putting all the department names in lowercase:
```
echo $dept | tr [:upper:] [:lower:] >> depts
```
Similarly, you could use the **sed** command's **A-Z** and **a-z** strings to accomplish the same thing:
```
echo $dept | tr a-z A-Z >> depts
```
As you undoubtedly suspect, reversing the order of the a-z and A-Z strings will have the opposite effect, turning the text to all lowercase.
### Using awk
The **awk** command lets you do the same thing with its **toupper** and **tolower** options. The command in the script shown in the previous example could be done this way instead:
[][4]
```
echo $dept | awk '{print toupper($0)}' >> depts
```
The reverse (switching to lowercase) would look like this:
```
echo $dept | awk '{print tolower($0)}' >> depts
```
### Using sed
The **sed** (stream editor) command also does a great job of switching between upper- and lowercase. This command would have the same effect as the first of the two shown above.
```
echo $dept | sed 's/[a-z]/\U&/g' >> depts
```
Switching from uppercase to lowercase would simply involve replacing the **U** near the end of the line with an **L**.
```
echo $dept | sed 's/[A-Z]/\L&/g' >> depts
```
### Manipulating text in a file
Both **awk** and **sed** also allow you to change the case of text for entire files. So, you just found out your boss wanted those department names in all lowercase? No problem. Just run a command like this with the file name provided:
```
$ awk '{print tolower($0)}' depts
finance
billing
bookkeeping
```
If you want to overwrite the **depts** file, instead of just displaying its contents in lowercase, you would need to do something like this:
```
$ awk '{print tolower($0)}' depts > depts-
$ mv depts- depts
```
Making the change with **sed**, however, you can avoid that last step because **sed** can edit a file "in place" as shown here, leaving the file intact, but the text in all lowercase:
```
$ sed 's/[A-Z]/\L&/g' depts
```
### Capitalizing first letters only
To capitalize only the first letters of words in a string, you can do something like this:
```
$ echo design \& engineering| sed -e "s/\b\(.\)/\u\1/g"
Design & Engineering
```
That command will ensure that first letters are capitalized, but won't change the rest of the letters.
### Making sure only first letters are uppercase
It's a little more challenging when you want to change text so that only first letters are in uppercase. Say you're manipulating a list of staff members' names and you want them to be formatted in the normal Firstname Lastname manner.
##### with sed
You could use a considerably more complex **sed** command to ensure this result:
```
$ echo design \& ENGINEERING | sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g'
Design & Engineering
```
##### with python
If you have python loaded, you can run a command like this that also formats text so that only the first letters of each word are capitalized and the command may be a little easier to parse than the **sed** command shown above:
```
$ echo -n "design & engineering" | python3 -c "import sys; print(sys.stdin.read().title())"
Design & Engineering
```
There are many ways to change the formatting of text between upper- and lowercase. Which works best depends in part of whether you're manipulating a single string or an entire file and how you want the end result to look.
Join the Network World communities on [Facebook][5] and [LinkedIn][6] to comment on topics that are top of mind.
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3529409/converting-between-uppercase-and-lowercase-on-the-linux-command-line.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://www.flickr.com/photos/54027476@N07/4999959929
[2]: https://creativecommons.org/licenses/by-sa/2.0/legalcode
[3]: https://www.networkworld.com/newsletters/signup.html
[4]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE21620&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage)
[5]: https://www.facebook.com/NetworkWorld/
[6]: https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,161 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Converting between uppercase and lowercase on the Linux command line)
[#]: via: (https://www.networkworld.com/article/3529409/converting-between-uppercase-and-lowercase-on-the-linux-command-line.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
在 Linux 命令行中转换大小写
======
转换文本的大小写可能非常繁琐尤其是当你要避免无意间的拼写错误时。幸运的是Linux 提供了一些命令,可以使工作变得非常容易。
[andy.brandon50][1] [(CC BY-SA 2.0)][2]
有很多方法可以在 Linux 命令行中将文本从小写更改为大写,反之亦然。实际上,你有一组这样的命令可以选择。这篇文章检验了一些最佳的命令来完成这项工作,以及你该如何让它们正常工作。
### 使用 tr
**tr** translate 命令是在命令行或脚本中最容易使用的命令之一。如果你要确定要一串大写字符串,你只需将它传给 **tr**,如下所示:
```
$ echo Hello There | tr [:lower:] [:upper:]
HELLO THERE
```
[[Get regularly scheduled insights by signing up for Network World newsletters.]][3]
下面是一个在脚本中使用这个命令的例子,当你要确保添加到文件中的所有文本都使用大写形式以保持一致性时:
```
#!/bin/bash
echo -n "Enter department name: "
read dept
echo $dept | tr [:lower:] [:upper:] >> depts
```
将顺序切换为[:upper:] [:lower:] 会产生相反的效果,将所有部门名称都转换为小写:
```
echo $dept | tr [:upper:] [:lower:] >> depts
```
同样,你可以使用 **sed** 命令的 **A-Z****a-z** 字符串完成相同的操作:
```
echo $dept | tr a-z A-Z >> depts
```
毫无疑问,反转 a-z 和 A-Z 字符串的顺序将产生相反的效果,将文本全部变为小写。
### 使用 awk
**awk** 命令可让你使用它的 **toupper****tolower** 选项执行相同的操作。上例脚本中的命令可以用这种方式代替:
[][4]
```
echo $dept | awk '{print toupper($0)}' >> depts
```
相反操作(切换为小写)如下所示:
```
echo $dept | awk '{print tolower($0)}' >> depts
```
### 使用 sed
**sed** (流编辑器)命令也可用于切换大小写。它与上面显示的两个命令中的第一个具有相同的效果。
```
echo $dept | sed 's/[a-z]/\U&/g' >> depts
```
从大写字母切换到小写字母只需将行尾附近的 **U** 替换为 **L**
```
echo $dept | sed 's/[A-Z]/\L&/g' >> depts
```
### 操作文件中的文本
**awk** 和 **sed**都能更改整个文件的文本大小写。因此,你发现你的老板需要部门所有名字的小写么?没问题。只需带上文件名运行以下命令:
```
$ awk '{print tolower($0)}' depts
finance
billing
bookkeeping
```
如果要覆盖 **depts** 文件,而不仅仅是以小写形式显示,则需要执行以下操作:
```
$ awk '{print tolower($0)}' depts > depts-
$ mv depts- depts
```
但是,使用 **sed** 进行更改,你可以避免最后一步,因为 **sed** 可以“原地”编辑文件,如下所示,文件完整,但文本全部小写:
```
$ sed 's/[A-Z]/\L&/g' depts
```
### 仅首字母大写
要仅将字符串中单词的首字母大写,那么可以执行以下操作:
```
$ echo design \& engineering| sed -e "s/\b\(.\)/\u\1/g"
Design & Engineering
```
该命令将确保首字母大写,但不会更改其余字母。
### 确保只有首字母大写
当要更改文本以使只有首字母大写时,这更具挑战性。假设你正在处理一个工作人员姓名列表,并且希望以正常的“名 姓”方式对其格式化。
##### 使用 sed
你可以使用更复杂的 **sed** 命令来确保以下结果:
```
$ echo design \& ENGINEERING | sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g'
Design & Engineering
```
##### 使用 python
如果你已安装 python你可以运行这样的命令它还可以设置文本格式以便每个单词只有首字母大写并且它可能比上面显示的 **sed** 命令更易于解析:
```
$ echo -n "design & engineering" | python3 -c "import sys; print(sys.stdin.read().title())"
Design & Engineering
```
有多种方法可以在大小写之间更改文本格式。哪种方法效果最好取决于你要处理的是单个字符串还是整个文件,以及想要的最终结果。
加入 [Facebook][5] 和 [LinkedIn][6] 上的 Network World 社区,评论热门主题。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3529409/converting-between-uppercase-and-lowercase-on-the-linux-command-line.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://www.flickr.com/photos/54027476@N07/4999959929
[2]: https://creativecommons.org/licenses/by-sa/2.0/legalcode
[3]: https://www.networkworld.com/newsletters/signup.html
[4]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE21620&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage)
[5]: https://www.facebook.com/NetworkWorld/
[6]: https://www.linkedin.com/company/network-world