On 2011-10-27, Roland Küffner wrote:
My idea was to do it with some kind of dictionary file. In it each line would contain a single search replacement pair separated by tabs. Just like:

old term<tab>new term
some other random old text<tab>another replacement

OK, I went ahead and made a script that will handle mos situations.

Put this in ~/Library/Application Support/BBEdit/Text Filters (see NOTES below);

#! /usr/bin/perl
use strict;
my %dictionary = map {
    chomp($_);
    $_ ? (split /\t/ => $_, 2) : ()
} (<DATA>);
while (<>) {
    for my $search_term (keys %dictionary) {
      s/\b$search_term\b/$dictionary{$search_term}/ge;
    }
    print;
}
__END__
this    THAT
some    MANY
my  YOUR
up  DOWN
right   LEFT
north   SOUTH
wanted more NEEDED LESS
wanted  GAVE AWAY
on  OFF


## end of text filter script (don't include this line in script)

NOTES:
1. this will work on most text files. If the body of your text is large, you might get noticeable delay while it works, with the number of replacements also a factor.

2. The dictionary consists of the lines below the '__END__' line. You may have as many as you want. In this sample, I capitalized the replacement terms only to make them visible in my text while testing.

3. As written, this only replaces case-sensitively; if you want replacement regardless of capitalization, add an 'i' after the 'ge' at the end of the substitution line:
  s/\b$search_term\b/$dictionary{$search_term}/gei;

3. This has a little bit of error protection:
a. When the dictionary terms are split, it only splits on the FIRST tab, in case your replacement term has tabs in it (see the '2' in the split expression). b. the '\b' before and after the $search_term in the substitution line protects against replacing your search term when it happens to be part of a larger word. c. If your dictionary has any blank lines (after the end-of-line is chomped off), they will be ignored.

4. You could have more than one of these dictionary text filters, each with its own set of terms and replacements. Just put each one in its own file and save with a memorable name to your text filters folder. The only thing you would change would be what's below the '__END__' line;

5. My own preference would be to use a colon (':') rather than a tab between the search term and its replacement. And I would allow for but not require spaces on either side of the colon. In that case, the script would look like this:

#! /usr/bin/perl
use strict;
my %dictionary = map {
    chomp($_);
    $_ ? (split /\s*:\s*/ => $_, 2) : ()
} (<DATA>);
while (<>) {
    for my $search_term (keys %dictionary) {
      s/\b$search_term\b/$dictionary{$search_term}/ge;
    }
    print;
}
__END__
this:THAT
some:MANY
my:YOUR
up: DOWN
right:LEFT
north:SOUTH
wanted more:NEEDED LESS
wanted : GAVE AWAY
on:OFF



   - Bruce

_bruce__van_allen__santa_cruz_ca_

--
You received this message because you are subscribed to the "BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem, please email "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

Reply via email to