Search Designated Text File and Display Results

This macro makes it easy to search a designated plain text file on a whim, without having to open the file or even a text editor. It's most useful if, like me, you have a text file or two with lots of different entries separated by line.

Search Text File.kmmacros (6.4 KB)

While it should be useful as-is, I am running into one issue with it. If the search text appears in the chosen file more than once, it currently repeats the first line containing matching text it finds instead of showing all lines with matching text. As an example, if there are two lines in a file, one with "Macro Name" and one with "Macro Text", and you search for "Macro", instead of showing

Macro Text```

it shows

```Macro Name
Macro Name```

Any help figuring out what's causing this would be much appreciated.
1 Like

Can you give as your sample text/file to reproduce the problem?

(You can upload any file as zip here.)

Certainly. Here's a short test file I just whipped up using Lorem Ipsum: (449 Bytes)

There are three lines that contain "ut" in this file:

Ut enim ad minim veniam
ut aliquip ex ea commodo consequat```

But currently, using this macro to search this file for "ut" results in the first line repeated three times:

<img src="/uploads/default/original/2X/e/ea38b858f3d5a34c021a1de3b24224d2844e3d8d.png" width="518" height="253">

You have been searching in the wrong variable. Use MatchText instead of SourceText:

(In your current macro you are searching n-times the whole source text again, that’s why you get repeatedly the first matched line.)

I’ve also changed the first search expression so that it is case insensitive. (Judging by your second expression it’s that what you want.)

Search Text File [rev].kmmacros (6.6 KB)

And change the window height in the “Manipulate Display Text Window” action. Otherwise you won’t see all results.

1 Like

Hey Guys,

I'm not seeing why there's any need for a secondary search when The substrings is already doing all the work.


Search Text File.kmmacros (6.8 KB)


Good point :grinning:

Well I’ll be! I never would have realized that the second search action was entirely superfluous on my own. Thanks very much for showing me a more efficient way to do this, Chris! I looked over both the For Each and Substring Collection pages on the KM wiki when putting this together (since I’m still new to both actions despite using KM for over 5 years) but didn’t fully grasp how the actions worked. And thank you, Tom, for pointing out a different mistake I’d made with searching the wrong variable, and for reminding me about the (?i) regex flag! You were right, I did want a way to search case insensitively, but mistakenly thought it wasn’t possible to do so in the For Each substring action since there isn’t a native option for it. Thank you both again; your help is very much appreciated!

All the flags you find here should be usable in KM regexes.

1 Like

Thanks! I did know that those flags could be used in KM regexes, but I tend to forget about (?i) since I’m used to setting case sensitivity via a GUI :sweat_smile: (i.e. the checkbox in BBEdit’s Find window or dropdown menus in KM)

Thanks to KM8's new Prompt With List action, this whole macro has essentially been made obsolete in the best way possible. I've been able to pare it down from this:

to a single action:

There is a downside in that this version doesn’t support searching with regular expressions, but it more than makes up for that by being able to see and filter results instantly, and by being able to copy them to the clipboard with a single press of the Enter key (not to mention drastically simplifying the process of putting together a macro like this in the first place, making it easy to replicate the functionality for other files or lists). If anyone reading this is using the earlier version of this macro, or a similar macro, I highly encourage you to try a Prompt With List version instead.