Find and Replace multiple queries at once

99 views
Skip to first unread message

Pu Koh

unread,
Nov 5, 2021, 8:26:48 AM11/5/21
to BBEdit Talk
Is there a function to run multiple find and replace patterns instead of doing it one at a time?

Bruce Van Allen

unread,
Nov 5, 2021, 10:12:12 AM11/5/21
to bbe...@googlegroups.com
Pu Koh wrote on 2021-11-05 4:00 AM:
> Is there a function to run multiple find and replace patterns instead of
> doing it one at a time?
>

Look into BBEdit's Text Factories.

For more advice from this group, post an example of what you want to do,
with before and after samples.


--
    - Bruce

_bruce__van_allen__santa_cruz__ca_

jj

unread,
Nov 5, 2021, 1:24:59 PM11/5/21
to BBEdit Talk
Hi Pu Koh,

You could use BBEdit's Text > Canonize ... command.
Check "Canonize Using Grep Patterns" in the manual page 140.

With this sample transformation file (Note that find and replace expressions are separated by a tab):

# -*- x-bbedit-canon-case-sensitive: 1; x-bbedit-canon-match-words: 0; x-bbedit-canon-grep: 1; -*-

(foo)   f0zz\1
(bar|baz)   buzz
0   i

This text:

foobar
foobaz


Is canonized to:

fizzfoobuzz
fizzfoobuzz

HTH,

Jean Jourdain

Christopher Stone

unread,
Nov 5, 2021, 10:59:53 PM11/5/21
to BBEdit-Talk
On Nov 05, 2021, at 06:00, Pu Koh <p...@pukoh.com> wrote:

Is there a function to run multiple find and replace patterns instead of doing it one at a time?


Hey There,

Personally I would most often use a Text Filter and a little Perl, although it depends upon the context.  (See appended script.  See “Text Filter” in the manual if you don't know how to use them.)

NOTE: JJ's example text's tab separators got turned into spaces by the web. (At least in Apple Mail.)

Canon files can be a bit confusing, until you've used them a few times – but they can be very useful.

As Bruce suggested – a Text Factory 
is also a perfectly viable option.


--
Best Regards,
Chris



#!/usr/bin/env perl -sw

while (<>) {
    s/\bnuts\b/NUTS!/g;
    s/\bgoofy\b/GOOFY!/g;
    print;
}



Sample Text:



nuts are pretty nutty nuts
nutso

goof
goofy is as goofy does.



Message has been deleted

Pu Koh

unread,
Nov 5, 2021, 11:43:46 PM11/5/21
to BBEdit Talk
Thanks all. I shall look into the suggestions.
HEre's what I'm trying to do.
I'm creating HTML 5 banners, and need to inject code for the click tags.

The code looks like this, 


<!DOCTYPE html>
<!--
NOTES:
1. All tokens are represented by '$' sign in the template.
2. You can write your code only wherever mentioned.
3. All occurrences of existing tokens will be replaced by their appropriate values.
4. Blank lines will be removed automatically.
5. Remove unnecessary comments before creating your template.
-->
<html>
<head>
<meta charset="UTF-8">
<meta name="authoring-tool" content="Adobe_Animate_CC">
<title>BusSum22 P2 LB728x90 D1</title>
<!-- write your code here -->
<script src="BusSum22 P2 LB728x90 D1.js?1636109472417"></script>
<script>
var canvas, stage, exportRoot, anim_container, dom_overlay_container, fnStartAnimation;
function init() {
canvas = document.getElementById("canvas");
anim_container = document.getElementById("animation_container");
dom_overlay_container = document.getElementById("dom_overlay_container");
var comp=AdobeAn.getComposition("82825A3F9EA349F399DB9BAF8ABCE77F");
var lib=comp.getLibrary();
handleComplete({},comp);
}
function handleComplete(evt,comp) {
//This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
var lib=comp.getLibrary();
var ss=comp.getSpriteSheet();
exportRoot = new lib.BusSum22P2LB728x90D1();
stage = new lib.Stage(canvas);
//Registers the "tick" event listener.
fnStartAnimation = function() {
stage.addChild(exportRoot);
createjs.Ticker.framerate = lib.properties.fps;
createjs.Ticker.addEventListener("tick", stage);
}    
//Code to support hidpi screens and responsive scaling.
AdobeAn.makeResponsive(false,'both',false,1,[canvas,anim_container,dom_overlay_container]);
AdobeAn.compositionLoaded(lib.properties.id);
fnStartAnimation();
}
</script>
<!-- write your code here -->
</head>
<body onload="init();" style="margin:0px;">
<div id="animation_container" style="background-color:rgba(255, 255, 255, 1.00); width:728px; height:90px">
<canvas id="canvas" width="728" height="90" style="position: absolute; display: block; background-color:rgba(255, 255, 255, 1.00);"></canvas>
<div id="dom_overlay_container" style="pointer-events:none; overflow:hidden; width:728px; height:90px; position: absolute; left: 0px; top: 0px; display: block;">
</div>
</div>
</body>
</html>




And I need to injects bits in red

<!DOCTYPE html>
<!--
NOTES:
1. All tokens are represented by '$' sign in the template.
2. You can write your code only wherever mentioned.
3. All occurrences of existing tokens will be replaced by their appropriate values.
4. Blank lines will be removed automatically.
5. Remove unnecessary comments before creating your template.
-->
<html>
<head>
<meta name="ad.size" content="width=728,height=90">
<script type="text/javascript">
var clickTag = "XXXXXXXXX"; </script>
<meta charset="UTF-8">
<meta name="authoring-tool" content="Adobe_Animate_CC">
<title>BusSum22 P2 LB728x90 D1</title>
<!-- write your code here -->
<script src="BusSum22 P2 LB728x90 D1.js?1636109472417"></script>
<script>
var canvas, stage, exportRoot, anim_container, dom_overlay_container, fnStartAnimation;
function init() {
canvas = document.getElementById("canvas");
anim_container = document.getElementById("animation_container");
dom_overlay_container = document.getElementById("dom_overlay_container");
var comp=AdobeAn.getComposition("82825A3F9EA349F399DB9BAF8ABCE77F");
var lib=comp.getLibrary();
handleComplete({},comp);
}
function handleComplete(evt,comp) {
//This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
var lib=comp.getLibrary();
var ss=comp.getSpriteSheet();
exportRoot = new lib.BusSum22P2LB728x90D1();
stage = new lib.Stage(canvas);
//Registers the "tick" event listener.
fnStartAnimation = function() {
stage.addChild(exportRoot);
createjs.Ticker.framerate = lib.properties.fps;
createjs.Ticker.addEventListener("tick", stage);
}    
//Code to support hidpi screens and responsive scaling.
AdobeAn.makeResponsive(false,'both',false,1,[canvas,anim_container,dom_overlay_container]);
AdobeAn.compositionLoaded(lib.properties.id);
fnStartAnimation();
}
</script>
<!-- write your code here -->
</head>
<body onload="init();" style="margin:0px;">
<div id="animation_container" style="background-color:rgba(255, 255, 255, 1.00); width:728px; height:90px">
<a href="javascript:window.open(window.clickTag)"><canvas id="canvas" width="728" height="90" style="position: absolute; display: block; background-color:rgba(255, 255, 255, 1.00);"></canvas></a>
<div id="dom_overlay_container" style="pointer-events:none; overflow:hidden; width:728px; height:90px; position: absolute; left: 0px; top: 0px; display: block;">
</div>
</div>
</body>
</html>

Christopher Stone

unread,
Nov 6, 2021, 7:14:46 AM11/6/21
to BBEdit-Talk
On Nov 05, 2021, at 22:43, Pu Koh <p...@pukoh.com> wrote:
Thanks all. I shall look into the suggestions.
Here's what I'm trying to do.
I'm creating HTML 5 banners, and need to inject code for the click tags.
The code looks like this:


Hey There,

Here's a super basic proof-of-concept.

--
Best Regards,
Chris



#!/usr/bin/env perl -sw

my $headData = q(
<meta name="ad.size" content="width=728,height=90">
<script type="text/javascript">
var clickTag = "XXXXXXXXX"; </script>
);

$headData = substr($headData, 2, -1);

my $canvasWrapper01 = q(<a href="javascript:window.open(window.clickTag)">);
my $canvasWrapper02 = q(</a>);

while (<>) {
    s!<head>!$&\n$headData!i;
    s!(<canvas.+?</canvas>)!$canvasWrapper01$1$canvasWrapper02!i;
    print;
}



Pu Koh

unread,
Nov 7, 2021, 9:10:47 AM11/7/21
to BBEdit Talk
Thanks Chris,

Perl is too advance for my head to wrap around!

I ended up creating a canonize query for each size, and another to replace XXXXX with a URL (I can then easier edit the URL per job). Create a text factory for each size that run a set of canonize query. filtering by filename with the matching size.

Put the HTML files in a specified folder. Open up all the factories. run all the factories. put the HTML file back in the job folder. Done~

Not the most efficient. But it already saves me time!

Thanks everyone!

Reply all
Reply to author
Forward
0 new messages