Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2012
Registered User
 
Join Date: Dec 2010
Posts: 34
Thanks: 8
Thanked 0 Times in 0 Posts
[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 05:58 AM..
Sponsored Links
    #2  
Old 03-11-2012
Moderator
 
Join Date: Feb 2007
Location: The Netherlands
Posts: 7,505
Thanks: 73
Thanked 474 Times in 453 Posts
Have a look here:

only-uppercase-first-character
Sponsored Links
    #3  
Old 03-11-2012
Registered User
 
Join Date: Sep 2009
Posts: 103
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by louisJ View Post
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 09:37 AM..
    #4  
Old 03-11-2012
Registered User
 
Join Date: Dec 2010
Posts: 34
Thanks: 8
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #5  
Old 03-11-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,351
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
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

Sponsored Links
    #6  
Old 03-11-2012
Registered User
 
Join Date: Sep 2009
Posts: 103
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by Scrutinizer View Post
Or shortening the previous perl:

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

even better
btw my solution works fine with d'alpinisme
Sponsored Links
    #7  
Old 03-11-2012
Registered User
 
Join Date: Dec 2010
Posts: 34
Thanks: 8
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Scrutinizer View Post
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....
Sponsored Links
Closed Thread

Tags
awk gawk capital capitalize

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 06:12 AM.