Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 03-11-2012
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 1 September 2015, 9:23 AM EDT
Location: The Netherlands
Posts: 7,732
Thanks: 129
Thanked 549 Times in 516 Posts
Have a look here:

only-uppercase-first-character
Sponsored Links
    #3  
Old Unix and Linux 03-11-2012
tip78 tip78 is offline
Users Awaiting Email Confirmation
 
Join Date: Sep 2009
Last Activity: 13 March 2015, 11:44 AM 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 Unix and Linux 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 Unix and Linux 03-11-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 3 September 2015, 7:53 AM EDT
Location: Amsterdam
Posts: 10,436
Thanks: 359
Thanked 2,769 Times in 2,475 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 Unix and Linux 03-11-2012
tip78 tip78 is offline
Users Awaiting Email Confirmation
 
Join Date: Sep 2009
Last Activity: 13 March 2015, 11:44 AM 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 08:53 AM.