Description:

A one liner to produce Title Case (i.e. Set The First Letter Of Each Word Of A Phrase To Upper-case). This gives two examples:

  1. To title case a phrase specified in the same one liner
  2. To title case a whole file.
  3. Note:
    I set the split command to assume that words are separated by whitespace, commas, or dashes--but there's sure to be an exception,you see. This can be modified, though.

    My system wants me to use double quotes when I do a perl -e. This is why I use a qq at the end.

    update:As danger says, there is an existing FAQ on this at How do I capitalize all the words on one line?. I didn't turn it up in my initial search (I tried title case, upper case, etc.)

    thanks, danger and arhuman

Phrase in one-liner
perl -e "$_='ZENO WAS HERE';@ph=map {ucfirst(lc)} split(/[\s.,-]+/);pr
+int qq(@ph)"
Phrase in separate file
perl -p -e "@ph=map {ucfirst(lc)} split(/[\s.,-]+/);print qq(@ph)" c:\
+foo.dat

update: arhuman sent me this smart variation (it leaves a trailing space, but is shorter):

perl -e '$_="ZENO WAS HERE"; print map {ucfirst(lc)," "} split/\s+/'
Replies are listed 'Best First'.
Re: Title Case One-Liner
by danger (Priest) on Feb 09, 2001 at 22:00 UTC

    There are also variations of one of this faq's answers which are a little shorter and also adjustable:

    perl -le '$_="ZENO WAS HERE";s/([^\s.,-]+)/\u\L$1/g;print' perl -le '$_="DANGER was hERe too";s/(\S+)/\u\L$1/g;print'
      I suggest s/([^\s\w]*)(\S+)/$1\u\L$2/g; instead. It treats things like q{"not a question" folks} (which becomes q{"Not A Question" Folks}).

      japhy -- Perl and Regex Hacker
        The trick there is making sure the first character is a word character. Here's a shorter version of the same thing: s/(\w\S*)/\u\L$1/g;
        Nice, but both this and chipmunk's can't handle "just,another,list,of,words" (ie. punctuation without spaces)... why not just:
        s/(\w+)/\u\L$1/g;
        Update: eating self-served humble pie, never mind, this can't deal with "i promise i won't shouldn't can't reply without thinking..." :)