Mass Prepend to Text Files - (Resolved - easily)

Hello!!!

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.

Thanks in advance!

Probably something along the lines of within the directory with markdown files

for filename in *.md; do echo "$filename" > "$filename.tmp"; cat "$filename" >> "$filename.tmp"; mv "$filename.tmp" "$filename"; done

In English:

  • For every markdown file
    • echo (print) the filename in to a new file filename.tmp
    • cat (print contents of a file) the filename’s file contents in to filename.tmp in append mode (>>)
    • mv (move) filename.tmp to overwrite filename
1 Like

I get this. error:
-bash: syntax error near unexpected token do’`

Hmm. I’m not really sure why that’s not working. I’ve run on my machine multiple times without issues.

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.

thank you. I appreciate the tips. I just walked away from my computer and I will give it a try as soon as I can.

Running the last suggestion throws this warning:
-bash: /users/...../testfolder: is a directory

Reinstalled bash. Didn’t make a difference for the first suggestion. Went through a zsh shell, same result as above.

Thanks for your help. But it is clear there is something not quite right.

The script looks like it should do the job, so a few questions if I may?

  1. Does it update any files at all?
  2. Are all of the files in the same,single directory?
  3. Have you tried adding any logging? E.g. Echo out the filenames to a running text file.
  4. 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?

No, won’t even run.

Yes.

Don’t really know what that is.

Still gave the same result as above :point_down:

Okay, just to check, can you confirm that you have set the executable bit on the script to allow it to be run?

E.g. chmod +x prepend.sh to set it as well excitable for all (user, group, and other).

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 :point_down:

I think that you should just be able to tweak the first line in the for loop to his to drop the .md.

echo "${filename%.*}" > "$filename.tmp"
1 Like