Shell Script To Convert Lowercase To Title Case?
By Dave Taylor
Expert Author
Article Date: 2011-03-15
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... Comments
About the Author: Dave Taylor is known as an expert on both business and technology issues.
Holder of an MSEd and MBA, author of twenty books and founder of four
startups, he also runs a marketing company and consults with firms
seeking the best approach to working with weblogs and social networks. Dave
is an award-winning speaker and frequent guest on radio and podcast
programs.
AskDaveTaylor.com
http://www.intuitive.com/blog/
|