bash 将给定文件的首字母转换为小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10070415/
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
Convert first letter of given file to lower case
提问by Sijith
I want to convert the 1st letter of each line to lower case up to the end of the file. How can I do this using shell scripting?
我想将每行的第一个字母转换为小写,直到文件末尾。我如何使用 shell 脚本来做到这一点?
I tried this:
我试过这个:
plat=`echo $plat |cut -c1 |tr [:upper:] [:lower:]``echo $plat |cut -c2-`
but this converts only the first character to lower case.
但这只会将第一个字符转换为小写。
My file looks like this:
我的文件看起来像这样:
Apple
Orange
Grape
Expected result:
预期结果:
apple
orange
grape
回答by Mat
You can do that with sed:
你可以这样做sed:
sed -e 's/./\L&/' Shell.txt
(Probably safer to do
(这样做可能更安全
sed -e 's/^./\L&\E/' Shell.txt
if you ever want to extend this.)
如果您想扩展它。)
回答by codaddict
Try:
尝试:
plat=`echo $plat |cut -c1 |tr '[:upper:]' '[:lower:]'``echo $plat |cut -c2-`
回答by Fritz G. Mehner
Pure Bash 4.0+ , parameter substitution:
Pure Bash 4.0+,参数替换:
>"$outfile" # empty output file
while read ; do
echo "${REPLY,}" >> "$outfile" # 1. character to lowercase
done < "$infile"
mv "$outfile" "$infile"
回答by Jér?me Kunegis
Here is a single sed command that uses only POSIX sed features:
这是一个仅使用 POSIX sed 功能的 sed 命令:
sed -e 'h;s,^\(.\).*$,,;y,ABCDEFGHIJKLMNOPQRSTUVWXYZ,abcdefghijklmnopqrstuvwxyz,;G;s,\
.,,'
These are two lines, the first line ending in a backslash to quote the newline character.
这是两行,第一行以反斜杠结尾以引用换行符。

