bash 将文件中每一行的第一个字母更改为大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10006449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the first letter of every line in a file to uppercase
提问by Village
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.
Would become:
会成为:
The bear ate the fish.
The river was too fast.
- The document contains some special letters: 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, and z.
- The uppercase forms of these letters are: 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, and 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。
- 这些字母的大写形式是: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?
如何将文件中每一行的第一个字母更改为大写?
回答by The Nail
Use sed:
使用sed:
sed 's/^\(.\)/\U/' yourfile > convertedfile
Little explanation:
小解释:
- the
^represents the start of a line. .matches any character\Uconverts to uppercase\( ... \)specifies a section to be referenced later (as\1in this case); parentheses are to be escaped here.
- 的
^代表线的开始。 .匹配任何字符\U转换为大写\( ... \)指定稍后要引用的部分(如\1本例所示);括号在这里要转义。
Do nottry to redirect the output to the same file in one command (i.e. ) as you will lose your data. If you want to replace in the same file then check out joelparkerhenderson's answer.> yourfile
不要不尝试输出到同一个文件重定向在一个命令(即),你将失去你的数据。如果您想在同一个文件中替换,请查看 joelparkerhenderson 的答案。> yourfile
回答by glenn Hymanman
There's a few sed answers with s/^\(.\)/\U\1/. GNU sed also has a \udirective that changes only the next letter to uppercase, so
有一些 sed 的答案s/^\(.\)/\U\1/。GNU sed 也有一个\u指令,只将下一个字母更改为大写,所以
sed 's/./\u&/'
Although if the first character on a line is a space, you won't see an uppercase letter, so
虽然如果一行的第一个字符是空格,你不会看到大写字母,所以
sed 's/[[:alpha:]]/\u&/'
回答by Vijay
pearl.311> cat file1
linenumber11
linenumber2
linenumber1
linenumber4
linenumber6
pearl.312> awk '{print toupper(substr(sed -i -e 's/^\(.\)/\U/' file.txt
,1,1))""substr(function up { local c="" ; echo -e "$c" | tr '[a-z]' '[A-Z]' ; }
while read line
do
echo $(up ${line:0:1})${line:1}
done
,2)}' file1
Linenumber11
Linenumber2
Linenumber1
Linenumber4
Linenumber6
pearl.313>
回答by joelparkerhenderson
To change the file in place:
要就地更改文件:
while read x ; do echo "${x^*}" ; done < inputfile > outputfile
回答by pizza
You can put your special characters in place of a-z and A-Z
你可以用你的特殊字符代替 az 和 AZ
for f in 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, and z.
do echo "$f foo bar." ; done |
while read x ; do echo "${x^*}" ; done | head -15 | tail -6
回答by agc
Pure bash:
纯bash:
E, foo bar.
E, foo bar.
é, foo bar.
è, foo bar.
ě, foo bar.
ē, foo bar.
Test/demo (remove the code after donefor more complete output):
测试/演示(删除代码以done获得更完整的输出):
Output:
输出:
##代码##
