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/^(.)(.*)$/1L2/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.

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

Neat!

Inspired by Regexident’s answer at StackOverflow.

Enhanced by Zemanta

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.