Console utility for capitalising/initialising words in string - for bash script?


Hi,

I'm trying to do a translation to a text string in a bash script, to initialise each word in a string (so we're talking console standard input/output here).

I found the 'tr' utility and tried a few things including:

echo "Simon briggs" | tr ' a-z' ' A-Z'      *note the spaces*

produces "SIMON BRIGGS", when I really want "Simon Briggs"...

so it only appears to work on characters individually, when I want to check for a 'pair' of characters. This should hopefully explain what I mean at least.

Anyone have advice on a more suitable util - and perhaps some hints on the syntax required for it?


Cheers

Simon
simonbriggsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChrisDrakeCommented:
You probably want to use perl (specifically - "perl -e") ?  It's considerably more powerful, and understands all alphabets in all languages, instead of just English using ASCII


echo 'fred nerk' | perl -e 'undef $/;$a=<>; $a=~s/\b(.)/\u$1/sg; print $a';

What the above does is...


undef $/; # treats line breaks like spaces, instead of input terminators
$a=<>; # Gets the input
$a=~s/\b(.)/\u$1/sg; # Does the conversion*
print $a; # Outputs it

Here's the regular expression explanation:-


$a      # This has out input
=~     # Tells perl to convert it using the following regexp
s/       # Says we want to substiture stuff
\b(.)   # Matches any "word break" and grabs the next character
/        # End of the match separator
\u$1   # replaces what we matched with the uppercase version of it ($1 is the variable from the earlier matching brackets)
/sg;   # "s" means treat line breaks like spaces, "g" means do everything (not just the 1st)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ChrisDrakeCommented:
p.s. beware of things already containing quotes, apostrophies, and backtics - many people enjoy putting their names into web forms by typing things like:-

joe `rm -rf .` smith

and so on - which of course will erase your entire PC if you run it through any script without noticing :-)

Also - there's a difference between " and ' under unix: you probably meant ' and not " earlier - do this to see why:-
echo "fred $HOME"
 or
echo "fred `ls`"
0
simonbriggsAuthor Commented:

Thanks Chris,

I took my sweet time getting back to this, but that was a great answer, and some very useful hints hacked on at the end too. Apologies for taking so long, but the help and information is appreciated.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.