如何将文件每个句子中的第一个字母大写并将其他大写字母转换为小写字母(如果有)并将修改后的文本重写到同一个输入文件中?
输入文件:
这是测试文件。它用于测试脚本。它用于将大写字母转换为小写字母。
与sed
:
$ sed -r 's/(.*)/\L\1/;s/((^|\.)\s*.)/\U\1/g' file
This is the test file.It is used for testing a script.Which is used for converting capital letters to small letters.
# Save changes to file
$ sed -ri 's/(.*)/\L\1/;s/((^|\.)\s*.)/\U\1/g' file
像这样的东西?
awk '{sub($0,tolower($0)); sub(/./, toupper(substr($0,1,1)))}1' RS=. ORS=. file
输出:
This is the test file.It is used for testing a script.Which is used for converting capital letters to small letters.
这适用于以 . 结尾的句子。,但不适用于感叹号、问号等。
您可以在 Perl 中使用tr 子例程:
$ cat original-file.txt | perl -ne 'tr/A-Za-z/a-zA-Z/; print;' \
> new-file-with-toggled-case.txt
$ mv new-file-with-toggled-case.txt original-file.txt