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: 19 May 2014, 10:58 AM EDT
Posts: 36
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 offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 29 September 2014, 12:58 PM EDT
Location: The Netherlands
Posts: 7,717
Thanks: 123
Thanked 537 Times in 507 Posts
Have a look here:

only-uppercase-first-character
Sponsored Links
    #3  
Old 03-11-2012
tip78 tip78 is offline
Users Awaiting Email Confirmation
 
Join Date: Sep 2009
Last Activity: 13 September 2014, 12:15 PM EDT
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: 19 May 2014, 10:58 AM EDT
Posts: 36
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 offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 October 2014, 1:11 AM EDT
Location: Amsterdam
Posts: 9,487
Thanks: 280
Thanked 2,398 Times in 2,150 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
Users Awaiting Email Confirmation
 
Join Date: Sep 2009
Last Activity: 13 September 2014, 12:15 PM EDT
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: 19 May 2014, 10:58 AM EDT
Posts: 36
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 01:58 AM.