Neon Rocket

Even Neo is Impressed

RSS 2.0

Bulk fix a list of words to the proper case

A set of metal types

A set of metal types (Photo credit: Wikipedia)

Problem

Given a long list of upper-case words (one word per line), such as:

MARY
JOE
SCOTT
FRED
... (for thousands of rows) ...

You want to change this to

Mary
Joe
Scott
Fred
(etc)

Solution

Perl

Perl (Photo credit: Wikipedia)

Use perl from the command line. Assume all your names are in a file called ‘names.txt’. We’re going to send the output to a new file called ‘cap_names.txt’.

$ perl -pe 's/^(.)(.*)$/\1\L\2/g' names.txt > cap_names.txt

How this works

On the matching side:
^(.) → get the first character. The parenthesis allow us to refer to it as \1.

(.*)$ → get the rest of the line. The parenthesis allow us to refer to it as \2.

On the substitution side:

\1 → outputs that first character, as is.

\L\2 → outputs everything else, lower case (the \L makes everything in \2 lower case).

Neat!

Inspired by Regexident’s answer at StackOverflow.

Enhanced by Zemanta
Bryan