Cimmaron Holman
Cimmaron Holman

Reputation: 161

In Notepad++ how do I find the nth occurrence of a string

I have a huge text file where records are identified by the string MSH.

I need to find the 200th record. I am hoping that there is a regular expression I can use in Notepad++ that would enable me to find the 200th occurrence of the string MSH.

Upvotes: 15

Views: 15245

Answers (2)

Dmitrij Holkin
Dmitrij Holkin

Reputation: 2055

Place cursor after 200-th occurrence of MSH

^(.*?MSH){200}\K

enter image description here

Upvotes: 6

BunjiquoBianco
BunjiquoBianco

Reputation: 2034

If your file is just an enormous one-liner delimited by the string "MSH" you could use this in a regular expression find.

But, make sure your cursor is at the beginning of the file or it'll just search for the next 200th record from where you started!

Find:

((.*?)MSH){199}

This should highlight the first 199 records, so the next unhighlighted record is the 200th.


OR, take it a bit further!
Again, in a regular expression find replace, again make sure your cursor is at the beginning of the file.

Find:

((.*?)MSH){199}((.*?)MSH{1}).*

Replace:

$3

Should replace the entire contents of the window with just the 200th record.

N.B: This assumes that the string "MSH" is not part of any of the records in the file.


As a footnote, I strongly doubt any of this is quick over a large file. Scripting is almost certainly a better option. Or possibly even dropping it into Excel and using text-to-columns.

Upvotes: 13

Related Questions