Subroutine to Get The Last Line Containing Specific Text

Name: [SUB] Get Last Line Containing

Type: Text search.

Purpose:

This subroutine finds the last line containing a string in an input text.

Inputs:

Local__Input: The body of text to be searched

Local__TextToFind: the string to be found

Output:

%Variable%Local__Found%: The subroutine returns the line containing the last occurrence of the string to be found. If nothing is found, the empty string is returned.

Error Checking:

The subroutine terminates with an error when either or both of the inputs are empty.

Credits:

Tom Sheppard’s (@TWSheppard) post in the KM Forum Capture the last time code (for a given event type) in a log file - #25 by TWSheppard

This is the subroutine:

Download Macro(s): [SUB] Get Last Line Containing.kmmacros (6.5 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

and this is a macro to demonstrate the use of the subroutine:

Download Macro(s): Test [SUB] Get Last Line Containing.kmmacros (7.2 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2
5 Likes

You know I love a text-processing subroutine! :slightly_smiling_face:

Do you get the same result as I do with this?

Last Line Containing - TEST.kmmacros (21 KB)

Macro screenshot

Input:

1
2g
3
4g
5
6

Look for last line containing g

Output:

4g
5

Also, I hadn't seen the Assert action before. Is there a functional difference between this and an If/Else that leads to either empty action slots (if at the end of a macro) or a Cancel this macro action? I can see a few uses for it that might neaten up some of my macros.

1 Like

Yup - it's an error in the regex (of course). I've just corrected it in the original post.

You could replicate this with If/Else actions but that adds the distraction of the actual If/Else structure.

Asserts originate, I believe, in the computer programming world where at the start of a program you would set out what is needed for the subsequent code to operate correctly and if that is not satisfied to throw an error, making debugging much more straightforward. You could call it "defensive programming".

It seems to me particularly appropriate in a subroutine that expects certain inputs to work properly and to therefore fail rather than continue with erroneous inputs.

I've included the assert here because I'm trying to protect anyone who comes along and uses this subroutine but fails to provide the correct inputs.

2 Likes

Great, that works nicely! Very handy.

Thanks for the explanation. I often find myself using If/Else for this kind of thing, but Assert is so much neater. Lovely stuff!

2 Likes