Appending a string using Search and Replace

56 views
Skip to first unread message

Jason M. Kelly

unread,
Jan 13, 2020, 9:26:34 AM1/13/20
to BBEdit Talk

I am beginning a new project and attempting to use BBEdit prepare a data set for input into a database. For the first step, I would like to add a string to each text entry in all caps that does not have a first name after it. For example, using the sample from the data set below, I would like to transform the entry “ABBOTT” into “ABBOTT, UNKNOWN”


 

ABBOTT, EDWARD (c.1737–91), painter, of Long Acre, London; according to Redgrave, he travelled in France and Italy with the engraver William Wynne Ryland, i.e. c.1760; see Ryland.

 

ABBOTT, JOHN, merchant; see Francis Harriman

 

ABBOTT

1763 Naples (31 Dec.; Martin jnl.MSS)

 


I am able to search for each entry in all caps that is not followed by a first name using this GREP formula: ^[[:upper:]]+$

 

For the life of me, I cannot figure out how to use the REPLACE function to retain the original text and append a string. (e.g. to transform “ABBOTT” into “ABBOTT, UNKNOWN”) 

 

I’m sure it’s a really simple thing that I’m missing, but I have been trouble finding a solution online. 


I appreciate any help from this list. Thanks. 

Jean-Christophe Helary

unread,
Jan 13, 2020, 9:33:13 AM1/13/20
to bbe...@googlegroups.com
Jason,

This is pretty much exactly what has been discussed in a very recent thread. There must be a discoverability issue in the manual... :)

What you need to do is have BBEdit *remember* the match so that it can keep it as is in the replace string.

^([[:upper:]]+)$

replaced by

\1, UNKNOWN

what you need to read is in "Creating Subpatterns", p 186 of the manual and the following section "Using Backreferences in Subpatterns".

Et voilà !

Jean-Christophe
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/35e76da1-8142-4168-af9e-abdf529b0602%40googlegroups.com.

Jean-Christophe Helary
-----------------------------------------------
http://mac4translators.blogspot.com @brandelune


Jason M. Kelly

unread,
Jan 13, 2020, 11:25:50 AM1/13/20
to BBEdit Talk
Thanks so much, Jean-Christophe. I appreciate it!

Jason

ctfishman

unread,
Jan 14, 2020, 10:34:10 AM1/14/20
to BBEdit Talk
You can also use an ampersand in the replace string to indicate the entire contents of the match, ie:

&, UNKNOWN

will give you the same thing as the \1 version. The difference is the \1 version requires the match to be captured in parentheses and the & version does not.

Jason M. Kelly

unread,
Jan 14, 2020, 10:51:22 AM1/14/20
to BBEdit Talk
You’re all so helpful! Thanks!
Reply all
Reply to author
Forward
0 new messages