Method for finding the Nth occurrence of a string in a data file?

39 views
Skip to first unread message

Devon Hubbard

unread,
Mar 14, 2022, 1:52:59 PM3/14/22
to BBEdit Talk
We have the need to search through a ton of transactional data for things like the 200th occurrence of <Session> to dig into that specific node.  One of our team members is thinking of writing an XPath thing, but then we thought there might be a way to do with this in BBEdit.  Not through any grep magic I've been able to get working so far.  Maybe a cool extension out there that does this?  

thanks

Sam Birch

unread,
Mar 14, 2022, 2:48:46 PM3/14/22
to BBEdit Talk

Devon,

XPath is probably the right move here, to be honest.

That said, BBEdit can select everything up to (and including) the 200th occurrence of <Session> with this regular expression:

(?s)(.*?<Session>){200}

Hope this helps.
-sam

Devon Hubbard

unread,
Mar 14, 2022, 3:53:48 PM3/14/22
to bbe...@googlegroups.com
Thanks Sam.  That grep pattern is fantastic!  

We can have a small worksheet with the grep pattern. We’ll search for N-1, then in the worksheet select <Session> and cmd+E, toggle to the data file window and cmd+G to find the next one.   😎

If this becomes a needed routine we’ll create something with XPath.  

Thanks! 

dEVoN 



On Mar 14, 2022, at 11:48, Sam Birch <list....@munkynet.org> wrote:

Devon,
--


jj

unread,
Mar 14, 2022, 8:32:38 PM3/14/22
to BBEdit Talk
Hi Devon,

This AppleScript script should help you to select the inner contents of any Nth <tag>.

HTH,

Jean Jourdain

Devon Hubbard

unread,
Mar 14, 2022, 9:53:42 PM3/14/22
to bbe...@googlegroups.com
ahhhh … Super Cool !   Thanks Jean !!

works like a charm.  

jj

unread,
Mar 15, 2022, 5:20:10 AM3/15/22
to BBEdit Talk
Hi Devon,

Good to know it worked.
I just updated the Gist to try to fix a "macOS error code: -2706"  that could happen when running the script on big files.

Jean

Reply all
Reply to author
Forward
0 new messages