louisJ
1
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
system
3
perl -pe 's/\b./\u$&/g' file.txt
louisJ
4
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
system
6
even better
btw my solution works fine with d'alpinisme
louisJ
7
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
louisJ
9
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
1 Like
system
11
it's Perl not pearl
lol
D�tente can be done too but i'm lazy for that
1 Like
louisJ
12
Indeed i should have mentionner it earlier , sorry ...