Search and Replace a Special Number

I have a number of markdown files, some of which have a 12-digit number prefixed with§, like for example §202009031445. The 12-digit number varies between files.

I want to replace the special number with <!--§202009031447-->. In other words, I want to prepend each special number with <!-- and append with -->.

Note, in that folder there are filename of the format ABC_DEF 202009031451 or ABC_DEF_xyz 202009031451, etc. I do not want the filenames to be touched.

I am using KM 9.0.6 on macOS 10.14.6.

I have tried to put together a macro, but got stuck. This is what I have so far:

image

If I fill in the directory and a hotkey trigger, will that execute the macro? I suspect not, but don't know what to do next.

My apologies for the image, I had to make a screenshot myself to get the top part of the macro settings.

You were pretty close. Try this for the search and replace action:

Search and Replace.kmactions (521 Bytes)

@gglick: howdy, long time no speak :joy:

I imported the action, but how do I delete the action I had defined?

Heh, yes, it seems like (and practically was) only yesterday we were figuring out your notes issue, doesn't it? At least this one seems like it should be much more straightforward. Anyway, it's very easy to get rid of actions in a macro; just hit the Delete key after selecting them.

@gglick: no joy, nothing changed.

Did you include the action in the For Each loop? When asking for help with your own macros, it's best to upload them along with an image. You can find instructions for how to upload a macro to the forum here: https://wiki.keyboardmaestro.com/Forum#How_to_PostUpload_Your_Macro_to_the_Forum
It's also actually pretty easy to take an image of your entire macro. Just make sure the macro is selected in the editor's middle column and select Copy as Image from the Edit menu. Once it's copied, you can just paste it into a forum post to upload it.

@gglick: I discovered that my search term /\§\d{2,12}/ in my folder shows the files with those numbers, whereas your term §\d{12} does not yield anything.

I have to step away from my desk for a couple of hours.

Here's the macro
Bulk amend §.kmmacros (2.1 KB)

It wasn't working because the search and replace action wasn't included in the For Each action that handles the files. Instead of this:

you need to drag the search action into For Each so that it looks like this:

As for the search term, I don't know which app you're using it in, but in my testing, /\§\d{2,12}/ didn't do anything when used in KM, whereas §\d{12} successfully found the numbers in each test file I tried that contained them. Feel free to modify my term to §\d{2,12} if these numbers can be less than 12 digits, but since you specified that they were 12 digits numerous times in your OP, I thought doing the same for the regex would be more precise.

1 Like

@gglick: yes, that works, with YOUR search term :clap:
Many thanks, once again for your patience and help.

1 Like

@gglick: just out of interest, even though it is somewhat off topic, though not quite: are there different "dialects" of Regex? You know more about the topic than I do.

For example, the app I am using for which you wrote those 2 bulk macros is Obsidian. It's a plain text, markdown app. Recently the search function was upgraded, which included support for Regex. The Regex term I had come up with, /\§\d{2,12}/, did find all the notes with the relevant terms. But KM could not use it, nor could Sublime Text, which also supports Regex. ST could find them with your regex.

So, what's going on?

I'm actually not a huge regex expert, but yes, I do know that there are different flavors/dialects, and that the one KM, Sublime Text, and BBEdit uses is called PCRE, or Perl-Compatible Regular Expressions. I'm not 100% sure, but the one you were using in Obsidian looks like the kind used in JavaScript or PHP. If you're interested, KM's Help menu has a couple of links to regex resources relevant to its particular flavor.

@gglick: thanks, I understand. Regex, in whatever form/flavour/dialect, is a powerful search function, but it is like a whole new language one has to learn.

@gglick: sorry to bother you again. I just want to run this by you.
I the folder where I ran the last macro, there were 3 subfolders, all with non-markdown files, and none with any §12-digit numbers. So I thought that running the macro would leave files without those §12-digit numbers untouched.

Well, it did not, it deleted them, which surprised me of course. My questions:

  1. was it normal for the subfolders and contents to be deleted?
  2. if it was not normal, any idea why it could have happened?
  3. if it was normal, how could I have prevented it from happening?

BTW, I had all those files backed up so they are back in place.

Technically, KM uses the ICU Regular Expressions . While very similar to PCRE, there are some differences.

@Klaas1,

Generally if you develop and test a Regex using the Regex101.com site, using the "PCRE" flavor, it should work in KM.

1 Like

I tried the macro with a batch of test files and a subfolder myself, and can confirm it effectively deleted the subfolder's contents by turning the folder into a text file. It's not too hard to work around that, but it certainly isn't what I would have expected to happen. @peternlewis, can you confirm if this is the expected behavior, or a bug? I didn't see anything about the potential data loss dangers in the KM wiki article for the Search and Replace action, so I'm guessing it's the latter, but any confirmation/clarification you could provide would be welcome.

Steps to Reproduce

Run this macro:

Bulk Amend §.kmmacros (1.9 KB)

On a folder with one or more subfolders, like this:

Expected Results
Subfolder remains a folder, and its contents are not touched.

Actual Results
Subfolder is turned into a text file, and its contents deleted:

If you'd like to test this yourself, here's a copy of the folder and files/subfolder I used:

Test Folder.zip (2.8 KB)

In the meantime, @Klaas1, you can work around this issue by testing whether the file the macro is about to search is a folder or not like this:

Bulk Amend § (with folder workaround).kmmacros (3.0 KB)

@gglick: thanks for your feedback.

I am happy that my assumption the subfolders would be left untouched was not an unreasonable one.

Of course, I am not happy that subfolders and content disappeared.

I am happy I could recover the files.

I am not happy that my cloud storage app, SpiderOak, does not communicate with my computer properly anymore, and, although I am still awaiting feedback, I fear I will have to uninstall and reinstall. With S-O that is always a nightmare, at least in my modest experience.

The folder is not deleted per se, but trashed, so it is in the trash folder, but yes, the action (and several similar ones) should presumably fail if given a folder and I will adjust its behaviour for the next version.

1 Like

@gglick and @peternlewis,

Just a FYI: When I renamed a folder in the Finder to have a ".txt" extension, I got this:

image

While it remained and behaved like a Folder, it now looks like a text file:

image

Interesting, but that doesn't seem to be what happens with this macro (and no doubt with ones that use similar action, as Peter said). This is what the info window for the test folder I used shows:

Screen Shot 2020-09-07 at 14.22.29@2x

Once this happens, it no longer behaves as a folder, and there doesn't seem to be any way to return it to a folder or restore its contents short of finding them in the Trash (where, thankfully they do seem to be, again as Peter said). You can test this by selecting a folder you're willing to lose and running this simple macro (it doesn't even need any search or replace terms, at least in my testing):

Test Macro - Errant Folder Trashing.kmmacros (1.8 KB)