Occurrences of string in text file

I’d like to have KM read a text file and count the number of occurrences of a string variable in the file and pass that back as another variable. Can anyone suggest the easiest way to accomplish that?

Occurrences of string in text file

This should get you started. To use it, just select some text in a text editor, the hit Option-Command-T to bring up the prompt.

Tell it what to look for and whether or not to ignore case.

It will display a window that reports the number of occurrences using a variable for the count, as you specified.

I took the easy way out to get the source text (just select it) but you may prefer to select a file using the Finder. And what you do with the variable for the count can certainly be more exciting than printing it in a window.

But the meat of the macro is in the prompt and the shell script. Which I think is what you were after.

Number of Occurrences in Selection.kmmacros (5.8 KB)

1 Like

Here's another approach using JavaScript (JXA) that should do the trick.
Please let us know if either of these solutions work for you.

##example Results
(using text from file)

##Macro Library   @Strings Count Occurrences of SubString in String @Example

<a class="attachment" href="/uploads/default/original/2X/d/da92472aed953049b39aa78dc31a9b567590c56d.kmmacros">@Strings Count Occurrences of SubString in String @Example.kmmacros</a> (5.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


<img src="/uploads/default/original/2X/3/397dd3a455a1828511e5036eb94f95a71a85a7d2.png" width="532" height="1242">


###JXA Script

var kmeApp         = Application("Keyboard Maestro Engine");
var sourceStr       = kmeApp.getvariable('SCPT__SourceString');
var stringToFind   = kmeApp.getvariable('SCPT__StringToFind');

stringToFind       = escapeRegExp(stringToFind);

var matchList      = sourceStr.match(RegExp(stringToFind,"gi"));
var numFound       = (matchList) ? matchList.length : 0;


function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
1 Like

Thanks so much guys. I will report back with the results.

Thanks mrpasini and JMichael. I ended up trying JMichael’s method first and it seems to be working perfectly. Thanks to both of you!

This is pretty easy to do with straight Keyboard Maestro actions.

Note that both @mrpasini will process regular expressions (so if you string contains unusual characters that may be a problem). @JMichaelTX's solutions escapes them, so that should be fine. In my solution, you select the search matching in the For Each action.

Keyboard Maestro Actions.kmactions (2.2 KB)


This version gives you the option of automatically escaping metacharacters to avoid any confusion. Just for fun.

Number of Occurrences in Selection.kmmacros (6.4 KB)

1 Like

Pretty easy and pretty too :blush: .

I need to count the number of occurrences of a word on a Web page — what Safari reports as "matches" after executing "Edit ▹ Find ▹ Find ... ". Is the number of matches reported by Safari available to Keyboard Maestro? (Alternatively, I will search the Source Code ... which introduces wrinkles.) Thx.

In my above macro:
MACRO: @Strings Count Occurrences of SubString in String @Example

Use this for the SCPT__SourceString variable:


Disable the other Actions that set that variable.
Goto the web page of interest, and trigger the macro.


:smiley: :tada:

Works great — kudos and thanks.

I'm putting the macro I ended up here in case anyone else wants to use or peruse it.

Keyboard Maestro 8.0.4 “Count occurrences of search string in text of current Web page” Macro

Count occurrences of search string in text of current Web page.kmmacros (26 KB)

1 Like