Bulk changes of markdown files

I need to change my YAML setup. Having 1500 notes, the manual route isn’t that appealing. What would you suggest?

This is what I have

---
titel: A note title
taggar: #atag #asecondtag
zid: [[202012151052 A note title|A note title]]
källa: <url>
---

And this is what I want:

---
title: A note title
tags: [atag, asecondtag]
zid: 202012151052
source: <url>
---

So, one the first line, changing “titel” into “title”. On the second, chagning “taggar” into “tags” and changing the tagging syntax. On the third, just keeping the unique id. And on the fourth, chagning “källa” into “source”.

I guess Regex and something like SublimeText are the tools I need?

You can easily do it using a small script with regular expressions and replacing it with the format you want, kinda like i suggested for your other question.


:warning: BACKUP YOUR VAULT before trying any of this. There’s a lot that can go wrong and changes are automatically saved when using this method.


I don’t know about Sublime Text, but VS Code will do the trick.

  1. Open the vault folder.
  2. Hit Cmd+Shift+F.
  3. Activate the regex option. (That’s the .* button to the right of the search field.)
  4. Paste the expressions below in their respective fields.
  5. Press Cmd+Alt+Enter to apply the changes across all files.
# find
---\ntitel: (.*?)\ntaggar: (.*?)\nzid: (.*?)\nkälla: (.*?)\n---

# replace with
---\ntitle: $1\ntags: $2\nzid: $3\nsource: $4\n---
4 Likes

Thanks, that solved the first part. But I realize that the remaining three issues are much harder:

After I used the markdown importer, because how I have my current notes formatted, I’ve multiple instances of links like this in my note:

[[YYMMDDHHmm A note title|A note title]] A note title

I need a RegEx that convert that into

[[YYMMDDHHmm A note title]]

In my YAML I need to convert

uid: [YYMMDDHHmm A note title|A note title]] 

into

uid: YYMMDDHHmm

And finally, also in YAML, change

tags: #tag1 #tag2 etc 

into

tags: [tag1, tag2, etc]

Any help much appreciated!

Backup your vault again and perform these replacements. After each one, go through your notes (if you have too many, just a nice random sample) to make sure there are no unexpected results. There probably will be.

1. Note titles

This assumes that the note title is exactly repeated in three places.

Find: \[\[(\d{10}) (.*?)\|\2\]\] \2
Replace: [[$1 $2]]


2. UID

Find: uid:.*(\d{10}).*
Replace: uid: $1


3. Tags

This will take two passes. A script would be more elegant, but this quick-and-dirty fix will probably do the job.

Replace “<space>” with and actual space. I couldn’t make it show in the code block.

1st pass:

Find: tags: #(.*)
Replace: tags: [$1]

2nd pass:

Find: (?<=tags:.*) #
Replace: ,<space>

1 Like

Thanks again! Much appreciated!

2 Likes

hey @macedotavares this regular expressions are incredibly useful.

I’ve tried to learn regular expressions in the past, but not sure what are good resources to learn from, any resources suggestions?
thanks

2 Likes

Hi, @santi.

Let me start by stating very clearly that I’m not even an intermediate regex user. I’m not being modest, here. It’s just that regex is so powerful that even a beginner can do amazing things.

I started to learn regex out of necessity, while doing some text cleanup that would otherwise be very tiresome. I found it so useful (and so much fun) that I kind of took it as a game I regularly play.

My advice is to start with practical examples; real-world problems, preferably something close to an immediate need of yours. Like finding all e-mail addresses in your vault. Or all dates. Checkout these awesome playgrounds:

They both contain lots of example patterns, they both explain to you what part of the expression is doing what. So just pick one (or both) and go crazy.

VS Code has very robust Regex capabilities (others too, but I prefer this one); opening a file and hitting Cmd+Shift+F will allow you to experiment freely (on your own, though).

This may seem dumb, but only after getting the very basics, I’d go over a tutorial. Rexegg has one of the best.

When you start to feel more confident, you can play Regex Golf, where the goal is to build the smallest possible expression that satisfies some given conditions.

And of course, feel free to ask for help.

10 Likes

thanks a lot @macedotavares for the awesome helpful reply. It helps a lot and I can’t to start practicing it! I appreciate the awesome suggestions, I’ll take on your offer and reach out if I need help!

Thanks!!

2 Likes

When I did a test run on a small sample two weeks ago, this part worked. But when I now try to do the large bulk of my notes, this step doesn’t match anything in my notes.

This is what my tag-line looks like in my YAML after the first pass

tags: [hubandref #u]

And this is what I use to search

(?<=tags:.*) #

Any ideas why it doesn’t work?

I just tested this in VS Code and a strange thing happens: if I perform a simple “Find” (Cmd+F), it works as expected; however, “Find in Files” (Cmd+Shift+F) throws the error

Regex parse error: lookbehind assertion is not fixed length.

Well, indeed it is not. But that didn’t seem to bother VSC 15 days ago.

I don’t know if this is something that got broken with a VSC update, or if I’m missing something. Anyway, let me see if I can come up with an alternative expression.

1 Like

I’m using Atom which throws the same error. Will try SublimeText.

Don’t bother. It’s the same.

Very strange. Today it suddenly worked.

Wut?:woozy_face:

Did you notice if VSC was updated?

I’m using Atom, but I don’t think it was updated. Very strange.

For members who may be looking to edit YAML as a Database or Table-like view, Obsidian DB Folder has been a helpful extension for me to see YAML of files in a folder in a Notion-Like database.

For bulk edits, I would still recommend using regex or a Python script using the Frontmatter Module, and Obsidian DB Folder is useful for visualizing YAML metadata.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.