Converting text files for import to OSX Notes

Discussion in 'Mac Apps and Mac App Store' started by nepentanova, Jun 18, 2016.

Tags:
  1. nepentanova macrumors member

    Joined:
    Aug 10, 2009
    #1
    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
     
  2. nepentanova thread starter macrumors member

    Joined:
    Aug 10, 2009
    #2
    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?
     
  3. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    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
     
  4. nepentanova thread starter macrumors member

    Joined:
    Aug 10, 2009
    #4
    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?

     
  5. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    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.
     

    Attached Files:

  6. nepentanova thread starter macrumors member

    Joined:
    Aug 10, 2009
    #6
    Hi Kryten, many thanks for that. How can i add a line break or carriage return after the inserted line?
     
  7. kryten2, Jul 6, 2016
    Last edited: Jul 6, 2016

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    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.
     
  8. nepentanova thread starter macrumors member

    Joined:
    Aug 10, 2009
    #8
    No problem. Many thanks Kryten
     

Share This Page