Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Shell script to convert lowercase to title case?

As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that converts a sentence of all lowercase to title case. You know, from "this is a test case" to "This is a Test Case". Not every word, just the right ones. Doable?


Dave's Answer:

That's an interesting project to chew on, actually, because there are a couple of very different ways to address the problem. Actually, that's pretty typical of Linux shell script programming because there are so many different commands in Linux.

The first thought I had was to use "sed" in a loop, basically doing something like "find [ ][a-z]" and replace it with "[ ][A-Z]" but that both strikes me as inefficient and a solution that'll make it just about impossible to skip words that shouldn't be capitalized.

Instead, the more logical solution seems to be breaking the sentence down into individual words, testing the words against the "skip" words, then fixing each of the remaining words before the fixed sentence is reassembled.

The trickiest part is to fix the first letter of the word, right? Or is it...

In fact, here's an easy way to break a word down into the first letter and the remaining letters:

firstletter=$(echo $word | cut -c1 | tr '[[:lower:]]' '[[:upper:]]')
otherletters=$(echo $word | cut -c2-)

It's one of my favorite commands, "cut", and we're using its ability to chop up what it's given character-by-character. Then "tr" transliterates lowercase to uppercase.

The main loop is pretty straightforward:

for word in $*
do
  per-word code goes here
done

The most interesting part is perhaps how to skip words that shouldn't be capitalized. After thinking about a couple of possibilities, here's what I came up with:

case $word in
  the|and|an|or|a|of) /bin/echo -n "$word "; continue; ;;
esac

The problem? If the first word is one of these stop words, it still needs to be capitalized, so it's a bit more nuanced: these words should only be skipped if they aren't the first word that appears in the sentence. Which means we're going to need to keep track of how many words we've scanned...

Easily done, though. Super easy. Just add a conditional around the "case" statement:

if [ $wordcount -gt 0 ] ; then ...

The fastest way to keep incrementing the counter variable is to use the shell's built-in mathematical capabilities:

wordcount=$(( $wordcount + 1 ))

That's 95% of things, so I'll let you put all the pieces together properly to get it to work...


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: Shell Script Programming , Unix and Linux Help   (Article 9839, Written by )
Tagged: linux scripting, programming, shell scripting
Previous: Fastest way to setup a new iPad 2 (iPad2)?
Next: Easiest way to update Google Chrome?




Reader Comments To Date:

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!
Rather amazingly, there are no comments on this article yet.

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.