Multiple find & replace, at once

Topy's picture

I have a long text document where I'd like to change few characters to a string of text. For example change "a" to "/a.alt" and "t" to "/t.alt". To do this properly, I need to execute the find&replace commands at once. How to do it in a text editor? Can it be done with greps? If so, could you help me with the commands? Is there any MAC text editors that can do multiple f&r commands automatically? I know there are workarounds for this in Fontlab and such, but I really need to do this in a text editor.

oldnick's picture

Problem: Search and replace a with a.alt, then search and replace t with t.alt yields a few a.alt.alt artifacts. I'm not sure that, even if there is such a thing, a simultaneous search and replace function would prevent this outcome. However, if you replace all your as and ts with different characters--say, dagger and daggerdbl--a subsequent search of † and replace with a.alt would avert the problem...

blank's picture

I do this sort of thing with the GREP functionality of Indesign's find & replace tool, which has a GUI for generating some of the regexp.

Theunis de Jong's picture

You need to state what text editor you are using. There is no such thing as "GREP" :-) There is a sort of basic functionality, but details in semantics and supported functionality differs wildly among GREP-aware editors.

Using InDesign's GREP syntax, something like this ought to work: search for

\b([at])\b

and replace with

$1.alt

-- note this will only change the two examples you provided, and "at the same time" as well. Somehow I don't think there is any text editor that allows you to specify a host of strings to change at the same time ...

Michel Boyer's picture

I don't think there is any text editor that allows you to specify a host of strings to change at the same time ...

As long as you host of strings can be specified by a regular expression, it seems to me that BBEdit GREP can do it (I had almost forgotten BBEdit ever existed, this question just reminded me).

Michel Boyer's picture

On the Mac, I use the line command sed. For instance, if the file test contains

ccommaaccent
gcommaaccent
kcommaaccent
scommaaccent
tcommaaccent

then here is how one can match all those "one letter" followed by a commaaccent and generate a sub (this is a trace of execution, i.e. a copy-paste of what I get on in my shell window):

586 % sed 's/\(.commaaccent\)/sub \1 by \1.alt ;/' test
sub ccommaaccent by ccommaaccent.alt ;
sub gcommaaccent by gcommaaccent.alt ;
sub kcommaaccent by kcommaaccent.alt ;
sub scommaaccent by scommaaccent.alt ;
sub tcommaaccent by tcommaaccent.alt ;

Also, sed allows scripting. Very handy when many substitutions are to be done.

Michel Boyer's picture

Given an input file in.txt containing "a t a t", following Theunis' idea (with the sed syntax).

sed 's/\([at]\)/\1.alt/g' in.txt

gives the output a.alt t.alt a.alt t.alt; here there is just one rule. If there is more than one substitution rule, it gets more tricky; substitutions apply sequentially. Again with the sed syntax, if the letters are alone on their line (in this example blanks are not allowed) then

sed 's/^a$/a.alt/; s/^t$/t.alt/' in2.txt

will do the job: for the substitution to apply, the rules for a and t specify that a and t must be at the beginning (^) and end ($) of line so that, if the input file in2.txt is

a
t

the output will be

a.alt
t.alt

Topy's picture

Theunis: I have no specific text editor in mind, however I'm ready invest few bucks if there is one that does the job. I found out that in Windows there is a program called wReplace that does this exact thing.

Michel: Well this is excellent!

sed 's/\([at]\)/\1.alt/g' in.txt

This command seems promising. There are no separate characters I need to replace, they are in long text with complete words, no linebreaks or anything like that. Only thing is that I can't get the slash to work! The output must be: /a.alt

Theunis de Jong's picture

My personal favourite plain text editors are TextPad for Windows and TextWrangler for Mac.

With TP you can select between POSIX and Unix style GREP (although I have no idea what the difference is), and TW has very useful syntax coloring in its entry field.

Michel Boyer's picture

To get /a.alt, you need to "escape" the / in the resulting string, i.e. write \/; here is the trace on in.txt with the line a t a t:

508 % sed 's/\([at]\)/\/\1.alt/g' in.txt
/a.alt /t.alt /a.alt /t.alt

Topy's picture

Sweet. Works perfect, thanks Michel & co for your help!

Michel Boyer's picture

Great! By the way, the pattern \b that matches a word boundary and was used by Theunis above is not available on the Mac sed. If you ever need it or one of the other GNU Sed extensions, you can install gsed from MacPorts, using the line command sudo port install gsed.

Syndicate content Syndicate content