Re: multi line/word find and replace?

578 views
Skip to first unread message

Thomas Fischer

unread,
May 13, 2013, 4:03:46 AM5/13/13
to textwr...@googlegroups.com
Hi Dean,

this is a typical question for scripting, and there are different options with TextWrangler, I use a Perl script.
The easiest way may be to use AppleScript:

set replaceList to {{"hello", "ciao"}, {"goodbye", "arrivederci"}, {"red", "rosso"}, {"car", "perch"}, {"van", "van"}, {"holiday", "vacanza"}}
tell application "TextWrangler"
tell window 1
repeat with thePair in replaceList
replace (item 1 of thePair) using (item 2 of thePair) options {starting at top:true, case sensitive:true, match words:true}
# Check the "Search Options" in TextWrangler's scipting dictionary!
end repeat
end tell
end tell
 
You might want to prune the replacement list from identical replacements first (van -> van). 
And if you want to do this regularly with different replacement lists then you may need to find an efficient way to tell the script which list to use, e.g. by reading a file and creating a list from the data read.

Best
Thomas


Am 13.05.2013 um 04:24 schrieb Dean Avanti:

textwrangler does find and replace for single words, but how about lists of words. Lets say I wanted to translate words in a webpage, I dont want to do one at a time, I want to batch replace words.

for example I add a list

hello
goodbye
red
car
van
holiday

and the words I want to replace them with

ciao
arrivederci
rosso
perch
van
vacanza

I press a button and it changes each word for the one on the right.
Is there an option to replace 'more than one word at a time' and swap 'lists of words'.

(note to mods, Ive posted this question before but it didnt show)

--
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.
 
 

Christopher Stone

unread,
May 13, 2013, 10:16:18 AM5/13/13
to textwr...@googlegroups.com
On May 12, 2013, at 21:24, Dean Avanti <in...@avantivitastudios.com> wrote:
> Textwrangler does find and replace for single words, but how about lists of words. Lets say I wanted to translate words in a webpage, I dont want to do one at a time, I want to batch replace words.

______________________________________________________________________

Hey Dean,

This is a job for a sed or Perl text filter. You can do it with Applescript, but it will be a bit slow and more cumbersome to write and maintain.

------------------------------------------------------------------------------------------

#! /usr/bin/env perl
use strict; use warnings;
#---------------------------

while (<>) {
s!hello!ciao!gi;
s!goodbye!arrivederci!gi;
s!red!rosso!gi;
s!car!perch!gi;
s!van!van!gi;
s!holiday!vacanza!gi;

print;
}

------------------------------------------------------------------------------------------

#! /usr/bin/env bash

sed '
s!hello!ciao!g
s!goodbye!arrivederci!g
s!red!rosso!g
s!car!perch!g
s!van!van!g
s!holiday!vacanza!g
'

------------------------------------------------------------------------------------------

In both script the 'g' flag stands for global (not just the first instance).

In the Perl script the 'i' flag stands for case-insensitive, and you may want to remove it and do case-sensitive substitution to properly deal with capitalization like so:

s!hello!ciao!g
s!Hello!Ciao!g

Text filters should be installed here:

~/Application Support/TextWrangler/Text Filters/

You run them from the menu {Text}-->{Apply Text Filter}-->{Filter Name}

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages