我需要帮助将输入文件中句子中第一个单词的首字母大写input.txt:
这是我的第一句话。这是第二句话。第一个就是第三个。
我想让输出在输出文件中看起来像这样output.txt:
这是我的第一句话。这是第二句话。第一个是第三个。
我需要帮助将输入文件中句子中第一个单词的首字母大写input.txt:
这是我的第一句话。这是第二句话。第一个就是第三个。
我想让输出在输出文件中看起来像这样output.txt:
这是我的第一句话。这是第二句话。第一个是第三个。
尝试这个:
sed -r "s/(^|\.\s+)./\U&/g" <input.txt >output.txt
bash version 4方法:
#!/usr/local/bin/bash
while IFS="." read -r -a line ; do
for ((i=0; i<${#line[@]}; i++)) do
if [[ $i > 0 ]]; then
temp=$(echo ${line[$i]/ /})
echo -n "${temp^}. "
else
echo -n "${line[$i]^}. "
fi
done
echo
done < file
方式呢awk?
$ awk -F"\. " '{OFS=". "}{for (i=0;i<=NF;i++) {sub(".", substr(toupper($i), 1,1) , $i)}} {print}' output.txt
This is my first sentence. And this is the second sentence. That one is the third.
-F"\. "将字段分隔符设置为.(点 + 空格)。{OFS=". "}将输出字段分隔符设置为.(点 + 空格)。'{for (i=0;i<=NF;i++) {sub(".", substr(toupper($i), 1,1) , $i)}}循环遍历每个字段,将它们的第一个单词大写。第一个字段是this is my first sentence,它只是大写this。