Grep: Delete everything that doesn't match.

66 views
Skip to first unread message

Michael Kocher

unread,
Nov 12, 2022, 4:18:56 PM11/12/22
to BBEdit Talk
I'm trying to use a multi-file search and replace using grep. I'm still pretty new to grep and BBEdit and could use some help.

I can isolate the text I want to keep using this:

<div class="page-content(?s).*?<div data-analytics-region="prev-next"

Unfortunately, that's the content I want to keep. Is there a way to find everything except what that line pulls up and delete it?

Thanks

Rich Siegel

unread,
Nov 12, 2022, 4:23:13 PM11/12/22
to BBEdit Talk
On 12 Nov 2022, at 16:09, Michael Kocher wrote:

> I'm trying to use a multi-file search and replace using grep. I'm still
> pretty new to grep and BBEdit and could use some help.
>
> I can isolate the text I want to keep using this:
>
> <div class="page-content(?s).*?<div data-analytics-region="prev-next"
>
> Unfortunately, that's the content I want to keep. Is there a way to find
> everything *except* what that line pulls up and delete it?

The "Extract" button might do the job for you here. (Leave the "Replace" field blank.)

R.

Michael Kocher

unread,
Nov 12, 2022, 7:24:05 PM11/12/22
to BBEdit Talk
The problem with Extract is that it makes a new file, and I have a thousands of files. Is there a way to do it within the original file?

Bruce Van Allen

unread,
Nov 12, 2022, 7:39:15 PM11/12/22
to bbe...@googlegroups.com
It sure looks to me like Text -> Process Lines Containing… could do what you want.

1. Choose Find lines that do not contain:

2. Put the pattern you mention in the box.

3. Choose Delete matched lines

4. Make sure Copy to clipboard and Copy to new document are NOT checked.

5. Click Process

Doesn’t that do what you want? (Test with sample copies of your files.)

An approach using Extract could be to script it and add a step to unlink/delete the original file after the new one is created with the extractions; once the original file is deleted, the new one can be re-named with the original name.

Of course, it’s easy to picture doing this operation with a short script in Perl or Applescript, saved as a BBEdit text filter.

HTH
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/d73aa5e5-11e5-4b3f-9bc8-30de68e2ccd2n%40googlegroups.com.


— Bruce

_bruce__van_allen__santa_cruz_ca_





Michael Kocher

unread,
Nov 13, 2022, 12:00:43 AM11/13/22
to bbe...@googlegroups.com
Great! Thanks so much. I was able to get something workable from this. 

You received this message because you are subscribed to a topic in the Google Groups "BBEdit Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bbedit/JdwTs_VSoik/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/EDC7782A-67D9-46B1-8A64-5F0C8449B50B%40cruzio.com.

Kaveh

unread,
Nov 13, 2022, 12:00:43 AM11/13/22
to bbe...@googlegroups.com
Could you paste a representative sample of the original?

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/d73aa5e5-11e5-4b3f-9bc8-30de68e2ccd2n%40googlegroups.com.
--
Sent from MetroMail

Bill Walker

unread,
Nov 14, 2022, 11:22:08 AM11/14/22
to bbe...@googlegroups.com
Micheal,


The Find options have an Extract button.  I think that will do what you want.

Kaveh

unread,
Nov 14, 2022, 11:31:20 AM11/14/22
to bbe...@googlegroups.com
Hi Bill 

Michael said above that he has 1000s of files so needs to alter each file, not extract into one file. ;-)



--
Kaveh Bazargan PhD
Director
Accelerating the Communication of Research
Reply all
Reply to author
Forward
0 new messages