New to Typophile? Accounts are free, and easy to set up.
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.
10 May 2011 — 12:10pm
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...
10 May 2011 — 12:20pm
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.
10 May 2011 — 2:22pm
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])\band 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 ...
10 May 2011 — 4:11pm
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).
10 May 2011 — 4:37pm
On the Mac, I use the line command
sed. For instance, if the filetestcontainsccommaaccent
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.
10 May 2011 — 7:31pm
Given an input file
in.txtcontaining "a t a t", following Theunis' idea (with thesedsyntax).sed 's/\([at]\)/\1.alt/g' in.txtgives 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 thesedsyntax, if the letters are alone on their line (in this example blanks are not allowed) thensed 's/^a$/a.alt/; s/^t$/t.alt/' in2.txtwill 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.txtisat
the output will be
a.altt.alt
11 May 2011 — 12:42am
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
11 May 2011 — 1:46am
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.
11 May 2011 — 3:07am
To get
/a.alt, you need to "escape" the / in the resulting string, i.e. write\/; here is the trace onin.txtwith the linea t a t:508 % sed 's/\([at]\)/\/\1.alt/g' in.txt
/a.alt /t.alt /a.alt /t.alt
11 May 2011 — 9:22am
Sweet. Works perfect, thanks Michel & co for your help!
11 May 2011 — 11:33am
Great! By the way, the pattern
\bthat 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 installgsedfrom MacPorts, using the line commandsudo port install gsed.