Bet*_*ane 2 linux command-line
请有人能够解释我如何将文本文件中的所有小写字符转换为大写,然后将其另存为新文件?我的文件名为 NewFile.txt,包含 500 行随机字符。
Sté*_*las 23
在 POSIX 工具箱中,有:
<input.txt tr '[:lower:]' '[:upper:]' >output.txt
Run Code Online (Sandbox Code Playgroud)
但是请注意,对于 GNU 实现,它仅适用于单字节字符;因此,例如在使用 UTF-8 字符集的语言环境中,仅适用于英文字母。
<input.txt awk '{print toupper($0)}' >output.txt
Run Code Online (Sandbox Code Playgroud)
也是 POSIX 并且可以与awk.
<input.txt dd conv=ucase >output.txt
Run Code Online (Sandbox Code Playgroud)
也是 POSIX,但没有多少实现会音译非 ASCII 字符。
<input.txt sed 's/.*/\U&/g' > output.txt
Run Code Online (Sandbox Code Playgroud)
在 GNU 中工作 sed,但sed仅(这\U不是标准的)。
和 perl:
<input.txt perl -Mopen=locale -pe '$_=uc' >output.txt
Run Code Online (Sandbox Code Playgroud)
那个不使用语言环境的toupper规则,所以可能会更好地处理像o?ce这样的词(将一个?字符转换为三个字符FFI¹)。
uconv,来自 ICU 项目应该非常擅长处理各种国际极端情况,并假设输入/输出以 UTF-8 编码(或任何uconv --default-code返回;尽管参见-f/--from-code和-t/--to-code选项以指定不同的输入和输出编码):
<input.txt uconv -x upper >output.txt
Run Code Online (Sandbox Code Playgroud)
在vim编辑器中,如果在文件的第一个字符(gg到达那里),请输入gUG以将所有字符转换为大写,直到文件末尾。然后:saveas output.txt保存到输出文件。
或者使用 any exorvi实现(虽然不是所有的都会处理非 ASCII 字符):(:%s/.*/\U&/并将:w output.txt编辑过的文件写入output.txt并:q!退出而不保存现在修改的输入文件)。
随着zsh壳:
zmodload zsh/mapfile
mapfile[output.txt]=${(U)mapfile[input.txt]}
# or (csh-style):
mapfile[output.txt]=$mapfile[input.txt]:u
Run Code Online (Sandbox Code Playgroud)
要从大写转换为小写,以防还不是很明显:
tr: 交换 [:lower:]和[:upper:]awk: 改变 toupper改为tolowerdd: 改变 ucase改为lcasesed/ ex/ vi:改变\U以\Lperl: 改变 uc改为lc。uconv: 改变 upper改为lowervim: 改变 gUG改为guG(这就是诀窍之一)。zsh:(U)改为(L),:u到:l。¹ C / POSIX toupper()/ towupper()API 一次只能将一个字符转换为另一个字符,因此在如何更改文本大小写方面受到限制。请参阅https://unicode-org.github.io/icu/userguide/icu/posix.html#case-mappings了解更多信息。
| 归档时间: |
|
| 查看次数: |
811 次 |
| 最近记录: |