将文件中每行的首字母更改为大写

编程入门 行业动态 更新时间:2024-10-24 13:18:21
本文介绍了将文件中每行的首字母更改为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要将文件中每行的首字母更改为大写,例如

I need to change the first letter of every line in a file to uppercase, e.g.

the bear ate the fish. the river was too fast.

将成为:

The bear ate the fish. The river was too fast.

  • 该文档包含一些特殊字母:a,a,á,à,ǎ,ā,b,c,d,e,e,é,è,è,ě,ē,f,g,h,i,i, í,ì,ǐ,ī,j,k,l,m,n,o,o,ó,ò,ǒ,ō,p,q,r,s,t,u,u,ú,ù,ǔ, ü,ǘ,ǜ,ǚ,ǖ,ū,v,w,x,y和z.
  • 这些字母的大写形式为:A,A,Á,À,Ǎ,Ā,B,C,D,E,E,É,È,Ě,Ē,F,G,H,I,I ,Í,Ì,Ǐ,Ī,J,K,L,M,N,O,O,Ó,Ò,Ǒ,Ō,P,Q,R,S,T,U,U,Ú,Ù,Ǔ ,Ü,Ǘ,Ǜ,Ǚ,Ǖ,Ū,V,W,X,Y和Z.
  • 如何将文件中每行的首字母更改为大写?

    How can I change the first letter of every line in the file to uppercase?

    推荐答案

    使用sed:

    sed 's/^\(.\)/\U\1/' yourfile > convertedfile

    小解释:

    • ^代表一行的开始.
    • .匹配任何字符
    • \U转换为大写
    • \( ... \)指定以后要引用的部分(在这种情况下为\1);括号要在这里转义.
    • the ^ represents the start of a line.
    • . matches any character
    • \U converts to uppercase
    • \( ... \) specifies a section to be referenced later (as \1 in this case); parentheses are to be escaped here.

    不要不尝试使用一个命令(即 > yourfile )将输出重定向到同一文件,因为这将丢失数据.如果要替换为同一文件,请查看joelparkerhenderson的答案.

    Do not try to redirect the output to the same file in one command (i.e. > yourfile) as you will lose your data. If you want to replace in the same file then check out joelparkerhenderson's answer.

更多推荐

将文件中每行的首字母更改为大写

本文发布于:2023-10-16 05:55:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为大   首字母   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!