Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 697
Cimmaron Holman Avatar asked Jan 14 '12 17:01

Cimmaron Holman


1 Answers

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.

like image 130
BunjiquoBianco Avatar answered Oct 11 '22 13:10

BunjiquoBianco