A way to merge files with the same ISO-8061 format date prefix?

What I’m trying to do

I am consolidating my various journal entries into my Obsidian vault in daily note format.

In this instance, I am dealing with a large group of entries that I have moved over from Day One using the Day One to Obsidian Script.

Each entry has a filename starting with YYYY-MM-DD, but entries on the same day also have a suffix a, b, c… depending on how many there are, so for example, if you had three entries for today (31 August 2021), they would be titled 2021-08-31, 2021-08-31a, and 2021-08-31b.

The files in the example above share the same date prefix (2021-08-31). Could anyone show me a way to automatically go through a folder of such files and merge every instance of a file with an additional letter suffix (e.g. 2021-08-31a), with the file that shares the same date prefix and has no suffix (e.g. 2021-08-31)?

I am running Obsidian 0.12.12 on macOS 11.5.2.

Things I have tried

I feel that this problem requires some manipulation of the markdown files on the system folder, either with Unix or a scripting language (AppleScript or JavaScript). I do not yet have the ability to do this. I have searched this forum and the web for ideas, but nothing specific has come up. I plan to learn the languages and find a way, but I am wondering whether someone here with the required fluency might be able to show me how.

This assumes (among other things) that:

  • all of your files are in 1 folder with no subfolders and no unrelated files (at least none that have similar names),
  • that your filename suffixes are single lower-case letters as described, and
  • there are no more than 26 suffixes per day.

I am not a Unix shell expert. I tested this on 6 example files.

  1. In Finder, open the folder that contains the folder that contains your files.

  2. Right-click the folder that contains your files and select Services > New Terminal at Folder to open a Terminal window.

  3. In a text editor, create a file called mergedays.sh and type the following into it:

#!/bin/zsh

# For each date from 2000-01-01 thru 2021-12-31 (including some impossible dates),
for date in 20{00..21}-{01..12}-{01..31}; do
# if any filenames are that date plus “a” as a suffix, then
    if [ -f "$date"a.md ]; then
# append all files whose names are that date plus a single-character suffix to the end of that date's un-suffixed file.
        cat "$date"?.md >>"$date".md
    fi
done

The comments (lines starting with hash symbol) each explain what the following line is doing. I can explain more if you like.

  1. In terminal, type chmod +x mergedays.sh and then Return. This gives the file permission to be executed (run) as a script.

  2. Backup your files!

  3. In Terminal, type ./mergedays.sh and then Return. This runs the script, which should do the thing you are trying to do.

1 Like

Thank you very much for the effort and the clear instructions, @CawlinTeffid. When running on my whole folder, the script seemed to work for only one or two files (I couldn’t tell which ones, I just saw the file count go down).

I then deleted all files except for a series of three named 2018-07-19.md, 2018-07-19a.md, 2018-07-19b.md. I ran the script again, but nothing happened…

Did you open any of the non-suffixed files or just rely on the file count? The script leaves the suffixed files where they were, so the file count shouldn’t change at all but the non-suffixed files should now include the content of the suffixed ones.

You can compare the current file list to your backup to see what files went missing (tho I have no idea why any of them would).

1 Like

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