LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2009, 09:11 PM   #1
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Rep: Reputation: 15
Capitalise Only First Letter Of Each Word In A Sentence


Hi,

ive searched around about this for a bit, but have been unable to find a working and suitable solution for me to do, what i require is to be able to change a string such as: thIs Is a TeSt to --> This Is A Test - AND place it in a variable - so far ive only succesfully been able to make the whole sentence uppercase or lowercase.

Im sure theres a simple solution, any help would be greatly appreciated
 
Old 02-13-2009, 10:32 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
I'd probably convert it to lowercase then cap (I'll leave you to assign it to a variable)
Code:
echo "this Is a TeSt" | sed -nr -e 's/.*/\L&/' -e 's/(^.|[[:space:]].)/\U&/gp'
 
Old 02-15-2009, 11:41 AM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Code:
MYVAR=$(echo "this Is a TeSt" | awk '{ for (x=1;x<=NF;x++) { printf("%s%s ",toupper(substr($x,1,1)),tolower(substr($x,2)))}}') 
echo MYVAR now is $MYVAR
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capitalise ONLY the first letter in Perl resetreset Programming 3 01-10-2009 11:19 AM
non-letter in a word malikah Programming 1 07-12-2008 09:05 PM
oh god, the three letter word, ATI onelung02 SUSE / openSUSE 8 10-12-2004 05:17 PM
/proc/version/four/letter/word opioid Linux - Security 2 02-27-2004 08:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration