Capitalize some words in a CSV file

501 views
Skip to first unread message

Toon van der Struijk

unread,
Oct 11, 2013, 10:55:10 AM10/11/13
to textwr...@googlegroups.com
Hi all,

I have a CSV file with a lot of columns.

The first column represents someone's firstname, the third column someone's lastname.
Both first- and lastname are not always entered with the first character in uppercase, eg.:

john,,Doe,
or
John,,doe,
or
john,,doe,

I want to change both to be capitalized and leave other data in the CSV untouched, like this:
John,,Doe,

When I do a standard 'Capitalize words' on this CSV file, well, all single words get capitalized. And I don't want that, because some column data needs to be left lowercase.
I think it can be done with Grep, but I don't know how to write the correct search and replace criteria.

Can someone help me out here?

Steve

unread,
Oct 11, 2013, 1:25:07 PM10/11/13
to textwr...@googlegroups.com
When you replace a "saved group," you can specify \U to make ALL letters capitalized, \u to capitalize only the first letter of the first word, and \L and \l to make them lowercase (in the same format/manner).

You would search for
^("[^"\r]*",|[^,"\r]*,)("[^"\r]*",|[^,"\r]*,)("[^"\r]*",|[^,"\r]*,)

and replace it with

\u\1\2\u\3

Of course, you will want to manually check that someone's name SHOULDN'T start capitalized (some foreign names like duPont or vonTrapp, for example), but that can be left up for an exercise for the reader ;-)

-Steve

Tom Humiston

unread,
Oct 11, 2013, 2:27:59 PM10/11/13
to textwr...@googlegroups.com
Here's the simplest approach I can think of: Write a pattern that matches the first three values and captures them via parentheses. Write a replacement pattern with \u in front of the first and third values:

(.*?,)(.*?,)(.*?,)

\u\1\2\u\3

If a value somehow has a comma within it or a line has fewer than three commas, that would throw things off. But given your sample data, those patterns work.

- TH
> --
> This is the TextWrangler Talk public discussion group.
> If you have a feature request or would like to report a problem,
> please email "sup...@barebones.com" instead of posting here.
> ---
> You received this message because you are subscribed to the Google Groups "TextWrangler Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to textwrangler...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages