What is wrong with tiny (g)awk line ?? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

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

Shell Programming and Scripting


Tags
awk gawk capital capitalize

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2012
louisJ louisJ is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 15 November 2012, 11:41 AM EST
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
Franklin52's Avatar
Franklin52 Franklin52 is online now Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 16 April 2014, 9:10 AM EDT
Location: The Netherlands
Posts: 7,667
Thanks: 116
Thanked 525 Times in 496 Posts
Have a look here:

only-uppercase-first-character
Sponsored Links
    #3  
Old 03-11-2012
tip78 tip78 is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 28 February 2014, 2:10 AM EST
Posts: 109
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
louisJ louisJ is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 15 November 2012, 11:41 AM EST
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
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 16 April 2014, 8:55 AM EDT
Location: Amsterdam
Posts: 8,871
Thanks: 230
Thanked 2,140 Times in 1,921 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
tip78 tip78 is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 28 February 2014, 2:10 AM EST
Posts: 109
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
louisJ louisJ is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 15 November 2012, 11:41 AM EST
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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



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