So I imagine that what I want to be done here will be quite simple for anyone with the appropriate skills.
I have roughly 4000+ .md files that I use in two different applications. One application accepts the file name as the note title, while the other requires the first line to be the title.
So, enter the challenge. I am looking to prepend the filename to the first line of each text file. I imagine this could be quite easy using the terminal, but I am not quite sure. I thought I could do this in ios Shortcuts, but the only way I know how to do this is by using the split text action, but in iOS 14 (yes, yes, yes - I know, beta, beta, beta - otherwise it is working quite nicely) the action is not functional.
Any ideas?? I have quite a collection of software utilities that could possibly do it on macOS, but I imagine a quick terminal command could take care of it.
I think I may have something wrong/installed that might cause and issue. I think I have seen this before. Outside of stock I have home brew installed. Though my experience with these tools is limited.
You can also try within the directory with all of the markdown files creating a file prepend.sh
with the contents:
#!/bin/bash
# For every markdown file
for filename in *.md; do
echo "$filename" > "$filename.tmp"; # echo (print) the filename in to a new file filename.tmp
cat "$filename" >> "$filename.tmp"; # cat (print contents of a file) the filename’s file contents in to filename.tmp in append mode (>>)
mv "$filename.tmp" "$filename"; # mv (move) filename.tmp to overwrite filename
done
And to run sh prepend.sh
Otherwise, maybe try reinstalling bash with homebrew or installing zsh and attempting it in a zsh shell.
The script looks like it should do the job, so a few questions if I may?
Does it update any files at all?
Are all of the files in the same,single directory?
Have you tried adding any logging? E.g. Echo out the filenames to a running text file.
Have you tried running it as./prepend.sh rather than sh prepend.sh? I’m wondering if sh might be forcing a default shell rather than the one specified in the file?
Finally got it to work. It seems the command I was using to get to the directory was not taking me all the way. So in all cases, there were either no .md or no prepend.sh. I should have been able to spot this. However, I learned a great deal here. I will be using this a lot in the future, is there also a way to lose these extension with the filename. It appends with filename.md and I was hoping to not have .md in my note titles