0

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. I'd appreciate any help!

combine multiple text files, _+ filenames_, into a single text file

EDIT: 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...

EDIT: Further details

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

EDIT: Solution, if not what I'd started looking for

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
Brett
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 19 '23 at 16:48
  • Hello. Question, not "article". Also, "doesn't work" is not a way to describe problems here. Please specifically state what happens and what should happen instead. – Destroy666 May 19 '23 at 17:06
  • Thank you for the advice, I should have been more specific. The exact issue I'm getting is when I post the code at the bottom of this message I get is 'find: illegal option --r'.
    
    do
    
        echo "* $file" >> target-file.org
    
        cat "$file" | pandoc -t org >> target-file.org
    
    done```
    
    – Brett May 19 '23 at 17:20
  • You should edit the question instead of commenting when adding details. Anyways, are you just pasting that to CLI? That's a script, not a one line command – Destroy666 May 19 '23 at 18:12
  • Thank you, I'll add that to the above, new to the website with this question. You are correct I was posting it to the CLI as I was hoping to test the code before putting it into my shell script, which I now see the folly of. I put it in a shell script and now I don't get that error, but I also don't get an output file. 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 pasted in there now. It says everything runs good, but I don't have 'target-file.org' anywhere. – Brett May 19 '23 at 18:54
  • Hard to say what else is wrong. Is it running in the correct working dir? You can always add a direct full path after find. Make also sure pandoc is installed, else remove that. part as the answer suggested. – Destroy666 May 19 '23 at 19:24
  • I'd removed the pandoc and that didn't change anything. As far as I could tell it was going to the correct working directory, but that may have been the issue. Honestly, I've bypassed it since I realized I could uncomplicate this for myself and just do it in two steps. Step one, add file name of document the top of each document, Step two merge them.
    for file in *.txt
    do
      ed -s $file < <(printf '%s\n' 1i "$file" . wq)
    done
    cat *.txt > Merged.txt```
    
    So that's working, I'll give up on what I had otherwise been trying, thank you for the help!
    
    – Brett May 19 '23 at 19:38

0 Answers0