On a Linux mailing list near me a user asked:

I have a list of company names all in upper case; one per line. Can I use a combination of sed, tr, and other tools to automate converting them all to mixed case (i.e., initial letter uppercase, all others lower case)? There must be a shell script already written to do this, but not in the reference books I have.

I immediately blasted out a knee jerk reply:

perl -ne 'print ucfirst lc;' FILENAME will do it.

And then proceeded to read the answers others had provided.

Some suggestions caused me to wonder where the virtue of Laziness is hiding:

perl -pe 'tr/A-Z/a-z/; s/(\S+)/\u$1/g;' <filename>

Others reminded me to think before posting

My sed-fu was deficient I guess (or my sed was) so I fell back to p +erl: If you want word-case perl -pe 's/ \b (\w) ([^\s]+) \b /\1\L\2/gx' # ONE COMPANY -> One Company (rather than One company)

Oh, yes, multi word company names. How ... almost all the blathering time.

So I went back to redo my original and came up with:

perl -i -ne 's/(\b\w)/{uc $1}/eg; print;' <FILENAME>

Which I'm content with. But...can you come up with something better?

There is one nit I have with that solution. RUN4LIFE translates to Run4life rather than Run4Life. My reading of `man perlre` doesn't turn up a \ code for alpha only. The systems I have available to me at the moment don't support [:class:] for trying that out.

Be Appropriate && Follow Your Curiosity

In reply to Case Munging by mikeraz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.