Replace first in file in multi-file search

74 views
Skip to first unread message

Neil Faiman

unread,
May 7, 2026, 9:29:51 AMMay 7
to BBEdit Talk Mailing List
Well, at least I read the fine manual and saved myself the humiliation of publicly asking whether there was a way to selectively search in the set of files that matched a previous multi-file search. (Of course there is. Just select the previous search results from the “Results Browsers” list in the “Search in:” chooser.)

But I couldn’t find this one in the manual. I have a bunch of HTML files. Some of them have multiple <section> elements. I want to add a property to the first <section> tag in each file. I.e., before

<section>
some content
<section>
inner content
</section>
</section>
<section>
more content
</section>

and after:

<section id=“main_content">
some content
<section>
inner content
</section>
</section>
<section>
more content
</section>

But the only choices seem to be replace al or replace none. Is there a clever hack to solve this (or better, another feature I’ve overlooked?

Thanks,
Neil Faiman

Jason Davies

unread,
May 7, 2026, 12:29:28 PMMay 7
to BBEdit Talk Mailing List

Far from being an expert but this looks amenable to GREP searching/replace.

Is there a way to regularly identify the first occurrence in each file? What comes immediately before it?

Cheers,

J

--
This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: <https://mastodon.social/@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 visit https://groups.google.com/d/msgid/bbedit/0E76497B-0BB8-4AB2-A2B2-C595E261F46F%40faiman.org.

https://orcid.org/0000-0002-7213-2147

Davies, J. P. (2025). Landscapes of Liminality: Towards a Map. In A. S. Webb (Ed.), 2025 New Horizons in Threshold Concepts (Vancouver B.C.).

Davies, J. P. et al (eds.). (2024). Threshold Concepts in the Moment. Brill.

Mosurinjon S., Davies. J. P. (2024) 'Secularism as a Universalising Threshold Concept' in Davies et al (2024), 72--85.

flet...@cumuli.com

unread,
May 7, 2026, 12:59:58 PMMay 7
to bbe...@googlegroups.com
You can do this with a search/replace. The first pattern matches the contents of the file before the first <section>. [\s\S] is used instead of period so it will match without finding the next repetition of the pattern. The question mark on the first grouping makes it match the first <section>.

Search pattern: ([\s\S]*?)<section>([\s\S]*)

Replace pattern: \1<section id=“main_content">\2

[fletcher]

Massimo Rainato

unread,
May 7, 2026, 1:14:14 PMMay 7
to bbe...@googlegroups.com
i hope that help,
use only <section> as search
use <section id="main_content">\1

page 221 manuale / in replacement patterns

\N nth captured substring, first only.

[mrai64]
> To view this discussion visit https://groups.google.com/d/msgid/bbedit/E3C3CFD9-09EE-4CE3-8854-7238E8446121%40cumuli.com.

Jason Davies

unread,
May 7, 2026, 1:26:38 PMMay 7
to bbe...@googlegroups.com

But this would replace every instance of "<section>", where he wants to replace only the first one...

Cheers,

Jason

On 7 May 2026, at 18:13, Massimo Rainato wrote:

i hope that help,
use only <section> as search
use <section id="main_content">\1

page 221 manuale / in replacement patterns

\N nth captured substring, first only.

[mrai64]

Il giorno 7 mag 2026, alle ore 18:59, flet...@cumuli.com ha scritto:

You can do this with a search/replace. The first pattern matches the contents of the file before the first <section>. [\s\S] is used instead of period so it will match without finding the next repetition of the pattern. The question mark on the first grouping makes it match the first <section>.

Search pattern: ([\s\S]?)<section>([\s\S])

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 visit https://groups.google.com/d/msgid/bbedit/0E76497B-0BB8-4AB2-A2B2-C595E261F46F%40faiman.org.

--
This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: https://mastodon.social/@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 visit https://groups.google.com/d/msgid/bbedit/E3C3CFD9-09EE-4CE3-8854-7238E8446121%40cumuli.com.

--


This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: https://mastodon.social/@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.

Rich Siegel

unread,
May 7, 2026, 1:58:08 PMMay 7
to BBEdit Talk Mailing List
On 7 May 2026, at 9:29, Neil Faiman wrote:

> Well, at least I read the fine manual and saved myself the humiliation of publicly asking whether there was a way to selectively search in the set of files that matched a previous multi-file search. (Of course there is. Just select the previous search results from the “Results Browsers” list in the “Search in:” chooser.)
>
> But I couldn’t find this one in the manual. I have a bunch of HTML files. Some of them have multiple <section> elements. I want to add a property to the first <section> tag in each file. I.e., before

According to the user manual, on page 205 there's a list of positional assertions, one of which is "\A" which matches the beginning of the file. So, you could probably use something like "\A.+?<section" to find the beginning of the first `<section` to occur in the file, and go from there.

R.

--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <https://www.barebones.com/>

Someday I'll look back on all this and laugh... until they sedate me.

Massimo Rainato

unread,
May 7, 2026, 2:01:18 PMMay 7
to bbe...@googlegroups.com
Text factory can help you to replace first only?

---
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.

GP

unread,
May 7, 2026, 3:02:38 PMMay 7
to BBEdit Talk
As Massimo Rainato suggested, you can use a text factory consisting of a "Run Unix Command" action with the "Options..." set to:

perl -pi -e '!$subbed{$ARGV} and s/<section>/<section id=“main_content">/ and $subbed{$ARGV}++'

That isn't the most elegant perl one liner, but it works to find the first line in a file (and only that first occurrence) containing the matching <section> and substitute that match with <section id=“main_content">.

For the files and/or folders you want to apply the text factory to, click on the text factory's "Choose..." button and make your selections. (The text factory's "Options..." button will open a dialog for specifying file filters, nested folder handling, and file changed options.)

GP

unread,
May 7, 2026, 5:47:16 PMMay 7
to BBEdit Talk

Expanding on Rich's suggestion, a BBEdit Find and Replace grep solution is:

Find:

\A(?s)(?!.*?<section id="main_content">)(.*?)<section>

Replace:

\1<section id="main_content">

This is probably a better solution than my previously posted perl one liner text factory in that it checks first there isn't already a <section id=“main_content"> {the negative lookahead assertion (?!.*?<section id="main_content">)} in the file. If there isn't one, it then proceeds on with finding the first <section>, and, if found, replacing it with <section id="main_content">.
Reply all
Reply to author
Forward
0 new messages