How Do I Get All Lines in a String that end With the same SubString? (RegEx, grep)

I am trying to set up a quick macro in KM that selects text then looks for any line that ends with “@focus”. Then, I want to build a list with just those found lines (and paste it back).

This is a workaround to TaskPaper as it allows marking items with tags but when you want to see just the ones you marked it also shows all the headings and sub-headings. I only want to end up with an actionable list and no headings, extra lines, etc.

I know grep is powerful and I think KM can do this - although searching through KM library I did not get any hits for “grep”.

I should think this is pretty simple but I do not know how I should search for any line ending with “@focus" and then selecting that line? Sending the results to a list should not be too hard either (although I am not sure how this is done I have seen it done in AppleScript before).

Would someone please be so kind as to comment on how such a thing could be done?

Thanks

This example may be close, not sure though:
http://www.regular-expressions.info/completelines.html

Trying: Simulate key strokes: (select all, copy), search Clipboard with RegEx \bfocus\b (using the @ symbol messed up the RegEx syntax). Saving capture groups to variable: Clipboard3, display Clipboard3.

Result, macro ran but produced no results (there should have been six lines with @focus)

grep is a Regular Expression tool.
See Regular Expressions [Keyboard Maestro Wiki].

This should get you started. It does NOT do the paste, but you can easily add that.

You can view the RegEx details at RegEx101.com.

##Macro Library   Process LInes Ending With String @RegEx

2017-02-02 19:25 CT

  • VER 1.1
  • Put the keyword to search for into a KM Variable to make it easier to change.

####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/3/3c1b0039ab5a7d4e9de3aa9266752aa2a8c37f39.kmmacros">Process LInes Ending With String @RegEx.kmmacros</a> (5.0 KB)

---

###Example Results

<img src="/uploads/default/original/2X/3/369699cbab75dda9df4f9ebbf6e5a6f7b1061e4f.png" width="382" height="217">



---

<img src="/uploads/default/original/2X/c/cbc87433efd84d9c5985ca13e1596c94987e417b.png" width="580" height="1223">

Please feel free to ask any follow-up questions.

@levelbest, I hope you don’t mind that I renamed your topic to be more descriptive of your actual question, and to make it easier for others to find in a search.

Not at all, thanks for the reply. Working on your example now.

Download KM macro example
"Oops! That page doesn’t exist or is private."

Are you clicking on this link:

Please try again. It works for me.

You confused me with your first block. You went through all lines and numbered them first? Assuming this was part of your process but not sure if this is how I should start? For one thing I wont always have the exact number of lines each time … working through your example assuming it is OK to skip that first box (?)

Do you mean this KM Action?

This is just SAMPLE TEST data I made up.
The only important part is those lines the END with your keyword, "@focus".

Replace the data in this Action with your actual data.

Not for me.

[quote=“JMichaelTX, post:8, topic:6225”]
Do you mean this KM Action? This is just SAMPLE TEST data I made up.The only important part is those lines the END with your keyword, “@focus”. Replace the data in this Action with your actual data. [/quote] Yes, that one. I am starting from what is on the clipboard. That was just your notes (test data) as it were? OK, thanks.

@levelbest,

OK, I just uploaded a new version into my original post above.
Please try the link again. It is a new URL.

[quote=“JMichaelTX, post:11, topic:6225”]
OK, I just uploaded a new version into my original post above.Please try the link again. It is a new URL.[/quote]

Thanks very much for all that. It did download the script after you updated it and with a little tweaking, it works perfectly. I really appreciate the help.

Great!

If one of the above posts solves your problem/question as originally stated, please check the "Solved" checkbox at the bottom of that post.

Otherwise, please post your remaining questions/issues about this problem.
If you have other questions, please start a new topic.

Done, and done. Thanks again.

1 Like