?
Solved

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

Posted on 2004-11-28
3
Medium Priority
?
534 Views
Last Modified: 2013-12-15

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
0
Comment
Question by:simonbriggs
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
ChrisDrake earned 400 total points
ID: 12694658
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
 
LVL 2

Expert Comment

by:ChrisDrake
ID: 12694695
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
 

Author Comment

by:simonbriggs
ID: 13556808

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

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question