将文件中每行的首字母更改为大写 [英] Changing the first letter of every line in a file to uppercase

查看:392
本文介绍了将文件中每行的首字母更改为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

    这篇关于将文件中每行的首字母更改为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆