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?

fwaechter
  • 885
  • 3
  • 10
  • 19

4 Answers4

17

Just capitilise all letters that follow a semicolon:

sed -e 's/;./\U&\E/g'
Bart Sas
  • 1,595
  • 11
  • 9
  • Thank you. That was exactly what i've searched. :-) – fwaechter Oct 06 '10 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: http://stackoverflow.com/a/2762997/521032 – Septagram Jun 03 '13 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"
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
enzotib
  • 1,596
  • 1
  • 8
  • 14
1
echo "a23;asd23;sdg3" | perl -ne 's/(?<=\W)(\w)/ uc($1) /gex;print $_'

a23;Asd23;Sdg3
eumiro
  • 207,213
  • 34
  • 299
  • 261
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
ghostdog74
  • 327,991
  • 56
  • 259
  • 343