Is it possible to write a code, telling MMA to join 2 (or even more) .txt-files without importing them?

  • $\begingroup$ Something like cat then? $\endgroup$ – J. M.'s discontentment Apr 7 '16 at 9:50
  • $\begingroup$ @J.M. hmm, don't know what cat means, except a pet... $\endgroup$ – Kay Apr 7 '16 at 10:32
  • $\begingroup$ cat is Unix-style command line command $\endgroup$ – Rom38 Apr 7 '16 at 10:34
  • $\begingroup$ sounds like this is what I mean, as I can force MMA to rename files, I thought this joining might also work somehow. Looks like there is no such command, isn't there? $\endgroup$ – Kay Apr 7 '16 at 10:36
  • 1
    $\begingroup$ cat: en.wikipedia.org/wiki/Cat_(Unix) Run: reference.wolfram.com/language/ref/Run.html Could be a goer? $\endgroup$ – Quantum_Oli Apr 7 '16 at 10:44

Lets say you have two files 1.txt and 2.txt. You open your terminal, go to the folder and type

cat 1.txt 2.txt > new.txt

It will give you a new file new.txt combining the content of 1.txt and 2.txt

But this is not an answer for mathematica community. Lets do the same thing with mathematica.

First I shall create several files.

Do[Export[ToString[i] <> ".txt", {Join[ConstantArray["blah", i], {"\n"}]}, "Table"], {i, 7}]

Now I have 7 files to combine. I am going to create the bash command for cat and then Run it.

com = "cat ";
Do[com = com <> ToString[i] <> ".txt ", {i, 7}]
com = com <> " > out.txt"

Now you have a file out.txt will everything.

| improve this answer | |
  • 1
    $\begingroup$ This kind of still isn't an answer for the MMA community and cat is platform specific. $\endgroup$ – LLlAMnYP Apr 7 '16 at 22:08
  • $\begingroup$ I agree @LLlAMnYP, but this is the best I can do with MMA without importing the file :| . $\endgroup$ – Sumit Apr 9 '16 at 11:26
Run["cat file1.txt file2.txt > newfile.txt"]

But be sure to set your working directory to the folder with the files in first:

SetDirectory["~/Desktop/Text Cat"]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.