1

I'd like to combine multiple text files, all ending in .txt, into the same document with the filename of each separating the contents of each. I've found ways to combine the contents, I've found ways to combine the filenames, but the closest I've gotten to doing both is the below superuser question, which is exactly what I want, but the comments suggesting how to use the code on MacOS doesn't seem to work, likely because the question is almost a decade old. I'm on mac OS Big Sur 11.2.3.


Update: At the advice of the bot, I've copied and pasted the clarifying details from the previous question posted, as it asked it very succinctly. Here is an example of what I am looking for

* a filename 
contents of file
... 
* another filename 
contents of file 
...

etc...

I'm running it through Automator, feeding the files in through "Get Specified Finder Items" and "Get Folder Contents" that then feeds "Run Shell Script" with this script copied below. It says everything runs good, giving 'workflow completed' and no errors, but I don't have 'target-file.org' anywhere.

find -regex '.*\.\(docx?\|org\|rtf\|te?xt\)$' | while read file
do
    echo "* $file" >> target-file.org
    cat "$file" | pandoc -t org >> target-file.org
done
Giacomo1968
  • 58,727
Brett
  • 11

1 Answers1

2

Solution as posted by Brett (question OP) converted to an answer.
Link to original version | CC-BY-SA 4.0


I realized I was overcomplicating this by trying to do it all in one step. I found some other stuff to run through automator's power shell that allowed me to add a file name to the top of each document, then when that was done combine all text with the very easy to use cat command.

Code that is currently working for me:

cd /Filepath_to_my_folder
for file in *.txt
do
  ed -s $file < <(printf '%s\n' 1i "$file" . wq)
done
cat *.txt > Merged.txt
Robotnik
  • 2,645