Making a Script That Transforms Sentences to Title Case?

I have a command that I use to transform sentences to title case. It is inefficient to have to copy this command out of a text file, and then paste it into the terminal before then also pasting in the sentence I want converted. The command is:

echo "my text" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'

How can I convert this to a script so I can just call something like the following from the terminal:

TitleCaseConverter "my text"

Is it possible to create such a script? Is it possible to make it work from any folder location?

0

3 Answers

Since bash's parameter expansion includes case modification, there's no need for sed. Just a short function:

 tc() { set ${*,,} ; echo ${*^} ; }

Test (don't use quotes, since a title is typically no longer than a sentence, it shouldn't matter):

tc FOO bar

Output:

Foo Bar

Fancy version that avoids capitalizing some conjunctions, articles and such:

ftc() { set ${*,,} ; set ${*^} ; echo -n "$1 " ; shift 1 ; \
        for f in ${*} ; do \
            case $f in  A|The|Is|Of|And|Or|But|About|To|In|By) \
                    echo -n "${f,,} " ;; \
                 *) echo -n "$f " ;; \
            esac ; \
        done ; echo ; }

Test:

ftc the last of the mohicans

Output:

The Last of the Mohicans 
3

How about just wrapping it into a function in .bashrc or .bash_profile and source it from the current shell

TitleCaseConverter() {
    sed 's/.*/\L&/; s/[a-z]*/\u&/g' <<<"$1"    
}

or) if you want it pitch-perfect to avoid any sorts of trailing new lines from the input arguments do

printf "%s" "$1" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'

Now you can source the file once from the command line to make the function available, do

source ~/.bash_profile

Now you can use it in the command line directly as

str="my text"
newstr="$(TitleCaseConverter "$str")"
printf "%s\n" "$newstr"
My Text

Also to your question,

How can I convert this to a script so I can just call something like the following from the terminal

Adding the function to one of the start-up files takes care of that, recommend adding it to .bash_profile more though.

TitleCaseConverter "this is stackoverflow"
This Is Stackoverflow

Update:

OP was trying to create a directory with the name returned from the function call, something like below

mkdir "$(TitleCaseConverter "this is stackoverflow")"

The key again here is to double-quote the command-substitution to avoid undergoing word-splitting by shell.

3

I don't have comment privs, but slight improvement to ManUnitedBloke's answer, this will handle contractions like "don't" and "who's".

echo "my text" | sed 's/.*/\L&/; s/[a-z']*/\u&/g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest