On 05/18/2018, at 11:10, Matthew London <[email protected] 
<mailto:[email protected]>> wrote:
> Turns out there are sometimes multiple lines both within my KEEP text and in 
> my DELETE text.


Hey Matthew,

That's easy enough.

Let's start off by using a test string that provides better feedback:


&&&
TEXT STRING I WANT TO KEEP 1
&&&
TEXT STRING I WANT TO KEEP 2
Keep 2, Line 2
Keep 2, Line 3
###
TEXT STRING I WANT TO DELETE 1
&&&
TEXT STRING I WANT TO KEEP 3
Keep 3, Line 2
###
TEXT STRING I WANT TO DELETE 2
Delete 2, Line 2
Delete 2, Line 3
&&&
TEXT STRING I WANT TO KEEP 4
Keep 4, Line 2
###
TEXT STRING I WANT TO DELETE 3


To delete the “I want delete” strings:

Find (first pass with regex - keeping the ampersands):

(?s)^###.+?(?=^&&&|\z)

Replace with nothing.


A simple Perl Script text-filter to do the same as the above.

** The commented-out line removes the ampersands if uncommented.

Each line of this script (other than print) acts exactly like making making a 
find/replace pass with BBEdit's Find dialog.

Perl's canonical search/replace syntax looks like this:

s/<search-pattern>/<replace-pattern>/<switches>

Personally I prefer to use exclamation points in place of the forward slashes, 
so I don't have to escape forward slashes in paths, etc.  (I find this syntax 
easier to read.)

s!<search-pattern>!<replace-pattern>!<switches>


#!/usr/bin/env perl -0777 -nsw

s!^###.+?(?=^&&&|\z)!!igms;
# s!^&&&\n!!gm; # <-- Uncomment this line to remove the &&& lines.
print;


Uncomment means to remove the hash mark from the beginning of the line - i.e. 
remove the comment symbol.


A simple sed text-filter to remove the desired text and leave the ampersands.

It finds the range of text between ^&&& and ^### – deletes the line with the 
hash marks – and prints the rest.  The syntax looks scary at first, but (this 
pattern) breaks down to something very simple.


#!/usr/bin/env bash
# Keep the ampersands.
sed -En '/^&&&/,/^###/{ /^###/d; p; }'


A simple sed text-filter to remove the desired text and remove the ampersands.

It finds the range of text between ^&&& and ^### – deletes the line with the 
ampersands – deletes the line with the hash marks – and prints the rest.


#!/usr/bin/env bash
Delete the ampersands.
sed -En '/^&&&/,/^###/{ /^&&&/d; /^###/d; p; }'


Like many things using text-filters in BBEdit can be a bit scary at first, but 
after a while it becomes routine – and they become another valuable tool in the 
toolbox.

--
Best Regards,
Chris


-- 
This is the BBEdit Talk public discussion group. 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>
--- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/bbedit.

Reply via email to