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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What is wrong with tiny (g)awk line ??
# 1  
Old 03-11-2012
[SOLVED] What is wrong with tiny (g)awk line ??

Hi
I wish to capitalize the first letter or every word in a text file with:
Code:
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

Last edited by louisJ; 03-12-2012 at 06:58 AM..
# 2  
Old 03-11-2012
Have a look here:

only-uppercase-first-character
# 3  
Old 03-11-2012
Quote:
Originally Posted by louisJ
Hi
I wish to capitalize the first letter or every word in a text file with:
Code:
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
Code:
perl -pe 's/\b./\u$&/g' file.txt


Last edited by tip78; 03-11-2012 at 10:37 AM..
tip78
# 4  
Old 03-11-2012
Thanks but the solution from this thread
Code:
    #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
# 5  
Old 03-11-2012
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:
Code:
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:
Code:
perl -pe 's/\b./\u$&/g' infile

# 6  
Old 03-11-2012
Quote:
Originally Posted by Scrutinizer
Or shortening the previous perl:
Code:
perl -pe 's/\b./\u$&/g' infile

even better
btw my solution works fine with d'alpinisme
tip78
# 7  
Old 03-11-2012
Quote:
Originally Posted by Scrutinizer
I am guessing the back reference \\1 does not get passed to the toupper function inside the replacement part of gawk's gensub function
indeed, this must be it.

tip78 ...the problem is with accentuated words lire Détente....your pearl gives DéTente....
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

what's wrong with my awk line?

] && { echo "The free mem need to be more than 2G" } (3 Replies)
Discussion started by: yanglei_fage
3 Replies

2. Shell Programming and Scripting

what is wrong with this expr line

It keeps giving me expr syntax error. expr "${etherline }" : ’.*no match found’ 2>&1 >/dev/null Thanks in advance. (1 Reply)
Discussion started by: shadow_boi
1 Replies

3. Shell Programming and Scripting

what is wrong with this line?

system ("$ssh '$perf_stats' < temp_pipe 2>&1 &"); I need to start and interact with my executable defined by perf_stats on a remote machine but not change my command line to that of the remote machine. temp_pipe is a node created by mknod -f temp_pipe (6 Replies)
Discussion started by: looza
6 Replies
Login or Register to Ask a Question