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:
- To title case a phrase specified in the same one liner
- To title case a whole file.
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.)
Phrase in separate fileperl -e "$_='ZENO WAS HERE';@ph=map {ucfirst(lc)} split(/[\s.,-]+/);pr +int qq(@ph)"
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 | |
by japhy (Canon) on Feb 10, 2001 at 00:46 UTC | |
by chipmunk (Parson) on Feb 10, 2001 at 02:44 UTC | |
by MeowChow (Vicar) on Feb 10, 2001 at 03:38 UTC | |
by japhy (Canon) on Feb 10, 2001 at 03:44 UTC |
Back to
Cool Uses for Perl