Multiple Find and Replace at once

2,607 views
Skip to first unread message

Mike W

unread,
Apr 10, 2018, 2:40:33 PM4/10/18
to BBEdit Talk

Is there a search pattern that would be able to do a Multiple Find and Replace at once?

I have a large file and dozens of items that need to be replaced.

Here are some examples:

find: "~NM1*87*2~N3*" and replace with "↵ BILLING ADDRESS: "

find: "~DMG*D8*" and replace with "↵ SUB DOB: "

find: "~~NM1*82*1*" and replace with "↵ PROVIDER NAME:  "

the list goes on and on...

any ideas- much appreciated - Thanks in advance!

Sam Hathaway

unread,
Apr 10, 2018, 2:54:38 PM4/10/18
to BBEdit Talk

I think you can do this with a Text Factory:

Hope this helps!
-sam

--
This is the BBEdit Talk public discussion group. If you have a
feature request or would like to report a problem, please email
"sup...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To post to this group, send email to bbe...@googlegroups.com.
Visit this group at https://groups.google.com/group/bbedit.

Screen Shot.png

Fletcher Sandbeck

unread,
Apr 10, 2018, 3:07:42 PM4/10/18
to bbe...@googlegroups.com
If there are a lot of replacements it might be easier to do a script. Open Script Editor and create a script like this with all your find/replace operations. This conducts its operations in the frontmost window.

tell application "BBEdit"
activate
replace "~NM1*87*2~N3*" using "↵ BILLING ADDRESS: " searching in text 1 of front text document options {starting at top:true}
replace "~DMG*D8*" using "↵ SUB DOB: " searching in text 1 of front text document options {starting at top:true}
replace "~~NM1*82*1*" using "↵ PROVIDER NAME: " searching in text 1 of front text document options {starting at top:true}
end tell

You can use GREP as well by adding a search mode option.

replace "~NM1\*87\*2~N3" using "↵ BILLING ADDRESS: " searching in text 1 of front text document options {search mode:grep, starting at top:true}

[fletcher]

Jeffrey Jones

unread,
Apr 10, 2018, 3:10:52 PM4/10/18
to bbe...@googlegroups.com
On Apr 10, 2018, at 2:39 PM, Mike W <ymwi...@gmail.com> wrote:

Is there a search pattern that would be able to do a Multiple Find and Replace at once?

I've never used the feature myself, so I might be misunderstanding how it works, but your task sounds like what the new Canonize command is for. (See the manual, BBEdit v12+.)

Mike W

unread,
Apr 10, 2018, 4:24:42 PM4/10/18
to BBEdit Talk
I've never used Scrip editor. the "↵" symbol i put in, I actually need it to "move to next line" in BBEdit I just do "command + enter" what do i put in the Script to move to next line?

I would much rather use Grep but I'm not sure I know how to create what you're describing. when i select Find Replace i only see the option for 1 search and only a Grep check box. No option to Start at top or do multiple at same time

Sorry if i dont make sense... Thanks for your help!

Mike W

unread,
Apr 10, 2018, 4:24:54 PM4/10/18
to BBEdit Talk
Are you talking about the chrome add-on? We cant do this due to HIPAA (they require access to all your files)
If you dont mean the add on I dont understand

Patrick Woolsey

unread,
Apr 10, 2018, 4:35:31 PM4/10/18
to bbe...@googlegroups.com
On 4/10/18 at 3:47 PM, ymwi...@gmail.com (Mike W) wrote:

>Are you talking about the chrome add-on? We cant do this due to
>HIPAA (they require access to all your files) If you dont mean
>the add on I dont understand
>
>On Tuesday, April 10, 2018 at 2:54:38 PM UTC-4, Sam Hathaway wrote:
>>
>>I think you can do this with a Text Factory:
>>

There's no Chrome involved. :-)

Text factories are a special type of BBEdit document that allow
you to define and apply multiple actions (such as 'Replace All')
without needing to write a script.

To get started, just choose File -> New -> Text Factory to have
BBEdit create a new text factory document. You can then add &
configure any desired number of 'Replace All' actions, click the
"Choose" button to select the files and/or folders that you want
the factory to process, and press the 'Run' button in the lower
right corner to run the factory.

[PS: You can find lots more info about creating and using text
factories in Chapter 5 of the included PDF manual; just choose
Help -> User Manual.]

Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com/>

Fletcher Sandbeck

unread,
Apr 10, 2018, 4:47:54 PM4/10/18
to bbe...@googlegroups.com
You should be able to put in a new line using \n.

replace "~NM1*87*2~N3*" using "\nBILLING ADDRESS: " searching in text 1 of front text document options {starting at top:true}

[fletcher]

Mike W

unread,
Apr 10, 2018, 5:08:53 PM4/10/18
to BBEdit Talk
This tool looks like the answer
The only thing missing is the ability to move to the next line.

Anyone know if there is a way to have it move to next line?

Christopher Stone

unread,
Apr 10, 2018, 5:38:17 PM4/10/18
to BBEdit-Talk
Hey Mike,

On 04/10/2018, at 14:47, Mike W <ymwi...@gmail.com> wrote:
Are you talking about the chrome add-on? We cant do this due to HIPAA (they require access to all your files) 

Type Cmd-Shift-? in any application to access the Help menu's Search field.

Do this in BBEdit and then type “factory”.

This should be a first step whenever you're looking for a feature in Mac software.

If you don't find it then take a minute and search the user manual.

Menu --> Help --> User Manual

A BBEdit Text Factory will probably be the easiest method for you to implementing a multi-find/replace action, but there are other methods available.

You'll have to learn a little bit but not a lot.

Search the user manual for “Text Factory”, and try searching BBEdit-Talk as well.



On 04/10/2018, at 15:06, Mike W <ymwi...@gmail.com> wrote:
I've never used Scrip editor. the "↵" symbol i put in, I actually need it to "move to next line" in BBEdit I just do "command + enter" what do i put in the Script to move to next line? 

I would much rather use Grep but I'm not sure I know how to create what you're describing. when i select Find Replace i only see the option for 1 search and only a Grep check box. No option to Start at top or do multiple at same time


That's Script Editor.app, and it's the AppleScript/JXA editor provided with the macOS.  (Use Spotlight to find it.)

I'd rather use AppleScript than a Text Factory any day, but then again I'm very familiar with it.

** Note that Fletcher's script below is not using RegEx (although it's possible to do so).

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

tell application "BBEdit"
    activate
    replace "~NM1*87*2~N3*" using "↵ BILLING ADDRESS: " searching in text 1 of front text document options {starting at top:true}
    replace "~DMG*D8*" using "↵ SUB DOB: " searching in text 1 of front text document options {starting at top:true}
    replace "~~NM1*82*1*" using "↵ PROVIDER NAME: " searching in text 1 of front text document options {starting at top:true}
end tell

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

On 04/10/2018, at 13:39, Mike W <ymwi...@gmail.com> wrote:
Is there a search pattern that would be able to do a Multiple Find and Replace at once?


I have a large file and dozens of items that need to be replaced.

Here are some examples:

find: "~NM1*87*2~N3*" and replace with "↵ BILLING ADDRESS: "

find: "~DMG*D8*" and replace with "↵ SUB DOB: "

find: "~~NM1*82*1*" and replace with "↵ PROVIDER NAME:  "


Are these regular expressions or literal strings?

For a huge mess of find/replace actions I personally would turn to Perl, because it's easy to write, read, and maintain.



#!/usr/bin/env perl -sw

while (<>) {

s!~~NM1\*82\*1\*!\n PROVIDER NAME:!g;

s!~DMG\*D8\*!\n SUB DOB:!g;

s!~NM1\*87\*2~N3\*!\n BILLING ADDRESS:!g;

print;
}



This would be used in a Text Filter as opposed to a Text Factory.

Each of the lines beginning with s! is a find/replace action, and the basic structure is this:

s!<search_pattern>!<replace_pattern>!g; <-- Global switch - try more than 1 replacement per line.

Not knowing if your strings are literal or not I've escaped the possible regular expression tokens.



BBEdit text-filters operate on the selection if there is one, otherwise they operate on the whole front document.

Text filters are installed here:

~/Library/Application Support/BBEdit/Text Filters/

Text filters are run from:

BBEdit Menu Bar > Text > Apply Text Filter > <your_filter>

And of course you can give them keyboard shortcuts in BBEdit's Menus & Shortcuts preferences.




Someone else mentioned the new Canonize command.  It lets you assign a bunch of literal text replacements in a file and then apply them to the front document.  (See page 127 of the user manual.)

This might just be easier than a Text Factory (for literal replacements).



Now you probably have more choices than you wanted.

Hang in there and learn something new.  😎

--
Best Regards,
Chris

Christopher Stone

unread,
Apr 10, 2018, 5:43:30 PM4/10/18
to BBEdit-Talk
On 04/10/2018, at 16:01, Mike W <ymwi...@gmail.com> wrote:
This tool looks like the answer 
The only thing missing is the ability to move to the next line.

Anyone know if there is a way to have it move to next line?


Hey Mike,

Use the linefeed token \n.

find-text \nReplace-Text

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages