Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nepentanova

macrumors member
Original poster
Aug 10, 2009
38
5
looking at migrating from notational velocity to the OSX notes app. There are a couple of things that I need to fix first.

  • Firstly, I need to insert the file name (preferably excluding extension) in the first line of the file. nvAlt uses the filename as note title, notes app uses the first line, so many notes are missing context when imported as is.

  • Secondly I wish to export the markdown formatted file to RTF or
    another format that the OS X notes app will recognise. I have been
    able to export a single file via pandoc.

    pandoc test.txt -f markdown -t rtf -s -o test1.rtf
How can i amend this command to run on a folder of text files, using the current filenames and exporting to FILENAME.rtf
 
Bump.

Looking at the first request, is it possible to batch edit a folder of text files to insert the filename in the first line of each file?
 
Looking at the first request, is it possible to batch edit a folder of text files to insert the filename in the first line of each file?

Yes, your mac has a lot of command line utilities to manipulate text files. You can use the Terminal application to do so. Or perhaps look at an application like TextWrangler. A good starting point would be to search the net for: insert filename in first line of text file

You might also want to check the Forum Rules regarding bumps.

Info : https://macrumors.zendesk.com/hc/en-us/articles/201265337-Forum-Rules
 
Apologies. Searching the web returns a few pages that i do not fully understand!

This page gives a couple of alternatives. The first suggest this code:

Code:
perl -i -pe 'BEGIN{undef $/;} s/^/$ARGV\n/' `find . -name '*.txt'`

However, this doesn’t work with files that have spaces in their name. And for those that it does work, it prepends "./" to the inserted text. Any ideas?

 
Any ideas?

You can try this in Terminal:

First cd into the directory with the text files eg :

Code:
cd /foo/bar/bla

You can type cd and drag the folder from Finder into the Terminal window. Notice there's a space between the cd command and the path!

Second copy and paste this line into Terminal :

Code:
lf=$'\n';for f in *.txt;do filename=$(basename "$f" .txt); sed -i.backup "1s/^/${filename} \\$lf/" "${f}"; echo Done ${filename};done

Make a new folder with a text file in it to test before trying this on all your text files. The filename without extension should be on the first line of the text file and a backup file with the original text is created with a .backup extension.
 

Attachments

  • Schermafdruk van 2016-06-22 01-36-26.png
    Schermafdruk van 2016-06-22 01-36-26.png
    131 KB · Views: 185
Hi Kryten, many thanks for that. How can i add a line break or carriage return after the inserted line?
 
Hi Kryten, many thanks for that. How can i add a line break or carriage return after the inserted line?
Try this:

lf=$'\n';for f in *.txt;do filename=$(basename "$f" .txt); sed -i.backup "1s/^/${filename} \\$lf\\$lf/" "${f}"; echo Done ${filename};done

I've been trying to answer your question since last Friday but there's something in that line that's causing an error when I put it in code tags. That's something I just recently found out so my apologies for being so late to reply.
 
Last edited:
  • Like
Reactions: nepentanova
Try this:

lf=$'\n';for f in *.txt;do filename=$(basename "$f" .txt); sed -i.backup "1s/^/${filename} \\$lf\\$lf/" "${f}"; echo Done ${filename};done

I've been trying to answer your question since last Friday but there's something in that line that's causing an error when I put it in code tags. That's something I just recently found out so my apologies for being so late to reply.

No problem. Many thanks Kryten
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.