如何将csv文件的第n列的首字母转换为大写?

时间:2019-03-27 23:10:47

标签: bash csv sed

csv文件中的第12列全为大写。有时该列有3个字,但大多数情况下只有1个字。

"one","two","three","four","five","six","seven","eight","nine","ten","eleven","TWELVE","thirteen"

我想将第四列转换为仅首字母大写

"one","two","three","four","five","six","seven","eight","nine","ten","eleven","Twelve","thirteen"

我可以将整列都转换为小写:

awk -F"," 'BEGIN{OFS=","} {$12 = tolower($12); print}' "$tmp_input3" > "$tmp_input4"

该命令给了我:

"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen"

如何使第12列的第一个字母大写?

2 个答案:

答案 0 :(得分:1)

您可以使用search("Learn Java OOPS",Enter.toString());

select
screen_date
from d8003
where convert(datetime, screen_date, 120) between '20180601' and '20181231'

也就是说,您将第一个字符用toupper(substr($12,1,1)) substr($12, 2)变为大写,然后从索引2开始附加其余字符。

请参见online awk demo

awk -F"," 'BEGIN{OFS=","} {$12 = toupper(substr($12,1,1)) substr($12, 2)}1' "$tmp_input3" > "$tmp_input4"

答案 1 :(得分:1)

这可能对您有用(GNU sed):

sed 's/[^",]\+/\L\u&/12' file

这会将第一个字符串(一个或多个字符长且既不是逗号也不是双引号)转换为小写,但第一个字符串将其大写。