9

I try to capitalize the first letter in a CSV which is sorted like this:

a23;asd23;sdg3

What i want is a output like this

a23;Asd23;Sdg3

So the first String should be as is, but the second and third should have a capitalized first letter. I tried with AWK and SED but i didn't find the right solution. Can someone help?

1
  • Thank you all. Bart Sas' answer fits perfect my needs. :-)
    – fwaechter
    Commented Oct 6, 2010 at 12:34

4 Answers 4

17

Just capitilise all letters that follow a semicolon:

sed -e 's/;./\U&\E/g'
2
  • Thank you. That was exactly what i've searched. :-)
    – fwaechter
    Commented Oct 6, 2010 at 12:33
  • 1
    Some explanation is in order, since regexes in sed have their own special features. \U converts all characters to the right to uppercase, & is (probably) the same as \0, \E unnecessarily stops conversion. Here's a link another answer that explains it: stackoverflow.com/a/2762997/521032
    – Septagram
    Commented Jun 3, 2013 at 12:39
7

Bash (version 4 and up) has a "first uppercase" operator, ${var^}, but in this case I think it is better to use sed:

sed -r 's/(^|;)(.)/\1\U\2/g' <<< "a23;asd23;sdg3"
1
  • 1
    Thanks for mentioning the ${var^}, it did the trick for me!
    – lfxgroove
    Commented Apr 10, 2013 at 16:23
1
echo "a23;asd23;sdg3" | perl -ne 's/(?<=\W)(\w)/ uc($1) /gex;print $_'

a23;Asd23;Sdg3
1
$ var="a23;asd23;sdg3"
$ echo $var | awk -F";" '{for(i=2;i<=NF;i++) $i=toupper(substr($i,i,1))substr($i,1) }1' OFS=";"
a23;Sasd23;Gsdg3

Not the answer you're looking for? Browse other questions tagged or ask your own question.