[BOUNTY] Simple bash script to add a line inside front-matter

So I have a directory with lots of .md files that look like this:

file-something-name.md
3330-file-another-name.md
a-yet-another-file-name.md

Inside each .md file is info that looks like this (the values are different but keys are same for each file):

---
title: “Something title”
date: "2020-05-08"
category: something
---

Some markdown text here.

Now, what I want to do is, loop through ALL the .md files and add inside each file a new line after the date key line, with this info:

slug: “/[NAME-OF-MD-FILE]/“

so the new file after the change will look like this:

---
title: “Something title”
date: "2020-05-08"
slug: “/file-something-name/“
category: something
---

There is one more thing - if the “slug” line already exists, it should skip that file and dont change anything as some files might have slug key already in their front matter.

I will PayPal you 20USD for this quick bash script. Comment in this topic you are interested and send me DM.

Hi there
here you go

for f in $(grep --include=*.md -riL "slug:" /your_directory/); 
do sed -i "/date:.*/aslug: \/$(basename $f)\/" $f;
done

C

Thanks but this does not work - I am trying to execute the script within the same directory where all the .md files are. When I execute it, nothing happens and no changes are made to .md files.

Also, what is your_directory and where it comes from? I understand I should change this bit? What should I change it to if the .sh file is in same directory where .md files are?

Can you DM me or email me at chesignup@gmail.com

1 Like

DMed.

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