Is there any command that can rename the post files which are weird encoded?

I have seen questions like these come up frequently in this forum. That makes me think that the likes of sed, awk and perl are a lost art :slight_smile:

Here’s a quickly cooked up find + sed one liner:

find . -name "*.md" | xargs sed -i.bkp '0,/^+++\s*$/! s/^+++\s*$/noauthor = true\n\0/'   

This is just a quick demo. You can get a robust version of this by converting that 1-liner to a little bash script with checks for stuff like “does the file already have noauthor in front-matter”, etc.

To try this out safely: Copy one or more of your current .md files to a temporary directory, say ~/temp/foo, and then run the above command only after cd -ing to that directory.


Warning

  • Works on all .md files in the current and nested subdirectories.
  • MODIFIES the found files, but also creates a backup of the original files by giving a .bkp suffix.
  • You will lose those backups (they will get overwritten) if you run the same command again.
  • To run this risk-free, run the command without the -i.bkp part.
  • It assumes that you have only 2 lines in each .md file that begin contain just +++. Nothing will happen if a .md file contains 0 or 1 lines with +++.
  • With great power comes great responsibility.

Above converts a file with front-matter:

+++
title = "Foo"
tags = ["abc", "def"]
draft = false
+++

to:

+++
title = "Foo"
tags = ["abc", "def"]
draft = false
noauthor = true
+++
1 Like