-4

如何将文件每个句子中的第一个字母大写并将其他大写字母转换为小写字母(如果有)并将修改后的文本重写到同一个输入文件中?

输入文件:

这是测试文件。它用于测试脚本。它用于将大写字母转换为小写字母。

4

4 回答 4

1

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
于 2013-02-18T18:13:32.823 回答
0

像这样的东西?

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.

这适用于以 . 结尾的句子,但不适用于感叹号、问号等。

于 2013-02-18T21:05:49.263 回答
0

如果您愿意,它在 perl 中非常简单:

s/(\w+)/\u\L$1/g

在此处此处查看与您的查询相关的内容

于 2013-02-19T06:11:05.190 回答
0

您可以在 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
于 2013-02-18T18:16:47.567 回答