What is wrong with tiny (g)awk line ??

Hi
I wish to capitalize the first letter or every word in a text file with:

gawk '{$0=gensub(/(\w)(\w*)/,toupper("\\1")"\\2","g");}' file.txt

But it doesn't work...:
target:
bla test Test TEST
yes nO Yes yes
restult:
bla test Test TEST
yes nO Yes yes
any idea? thanks

Have a look here:

only-uppercase-first-character

perl -pe 's/\b./\u$&/g' file.txt

Thanks but the solution from this thread

    #for ( i=1; i <= NF; i++) {
    #    sub(".", substr(toupper($i),1,1),$i)
    #    }  
    print

doesn't capitalize this for example: I need " D'Alpinisme " instead of " D'alpinisme "....

and I really need awk.

thanks

I am guessing the back reference \\1 does not get passed to the toupper function inside the replacement part of gawk's gensub function:

Alternatively:

awk '{for(i=1;i<=NF;i++)$i=toupper(substr($i,1,1)) substr($i,2)}1'  infile

but that would squeeze spacing..

Or shortening the previous perl:

perl -pe 's/\b./\u$&/g' infile

even better
btw my solution works fine with d'alpinisme

indeed, this must be it.

tip78 ...the problem is with accentuated words lire D�tente....your pearl gives D�Tente....

By the way, since you were using gawk, GNU sed can do this:

sed 's/\b./\u&/g' infile

It does seem to work with D�tente as does this perl:

perl -pe 's/\b[[:alpha:]]/\u$&/g' infile

and this GNU sed:

sed 's/\b[[:alpha:]]/\u&/g' infile

srutinizer your awk line doesn't work with d'Alpisme

---------- Post updated at 08:56 AM ---------- Previous update was at 08:53 AM ----------

it would be useful to the awk word match \w to create a Filed separator FS.

Correct that only works with space separation, but at that point it wasn't clear that you were going to transform French :wink:

1 Like

it's Perl not pearl
lol
D�tente can be done too but i'm lazy for that

1 Like

Indeed i should have mentionner it earlier , sorry :stuck_out_tongue: ...