Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Appending two text (non-data) files together

    I have a program that is writing out code that will eventually be used in a different software package. The code gets written in separate pieces, and unfortunately the logic is such that I can't just generate the pieces in the order I eventually want them to be in. Once done, I want to combine the pieces in the correct order, e.g. if one tempory file is called front and another is called back I want to combine them into frontback.

    Interactively this takes the better part of 30 seconds of copy and paste. But I would rather just have Stata generate everything in a single piece.

    I am thinking I could open the final output file as read write and then do file read and write commands (basically copying the files one line at a time into a new file) but is there something simpler? If these were data files I could just use the append command but if such an easy solution exists it is currently eluding me.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

  • #2
    Well if you're running Windows you can open a command window (-shell- from within Stata) and run
    Code:
    copy file1+file2 destinationfile
    (You can append as many files as you like with more "terms" connected by + signs.)

    Or, again in Windows, you can do
    Code:
    type file2 >> file1
    and file1 will now have the contents of file2 appended to it.

    Comment


    • #3
      Thanks! That is certainly simple. But I would like something that would work on other operating systems too. The other software, Mplus, works on

      Operating Systems
      • Microsoft Windows 7/8/10
      • Mac OS X 10.8 or later
      • Linux (tested on the following distributions: Ubuntu, RedHat, Fedora, Debian, and Gentoo)
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        In the past I've done something similar by writing my temporary files as Stata data files containing a single string variable, called, say, line. Then I use Stata to append them in the appropriate order, and the write the appended file as a text file.

        Alternatively, on Mac and Linux systems, text files can be appended in a shell window with
        Code:
        cat file1 file2 > destinationfile
        and
        Code:
        cat file2 >> file1
        as in Clyde's examples.

        Comment


        • #5
          Clyde's tip works! Or at least the version using the type command does.

          If I do -creturn list- the values include

          c(os) = "Windows"

          I am guessing I could do some sort of OS check and then use William's command for Mac and Unix.

          Thanks for the great help. Thanks to you two I have a fighting chance of getting to bed at a decent hour tonight.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            I rewrote the code as follows. It works ok in Windows but I don't have a Mac or Unix machine to test it on. Does it look ok? I have this feeling I should eventually come up with code that doesn't rely on OS calls at all but for now this is pretty easy.

            Code:
                if "`c(os)'" == "Windows" {
                    shell type `xback' >> `mfilename'.inp
                }
                else if "`c(os)'" == "MacOSX" {
                    shell cat `xback' >> `mfilename'.inp
                }
                else if "`c(os)'" == "Unix" {
                    shell cat `xback' >> `mfilename'.inp
                }
            -------------------------------------------
            Richard Williams, Notre Dame Dept of Sociology
            StataNow Version: 19.5 MP (2 processor)

            EMAIL: [email protected]
            WWW: https://www3.nd.edu/~rwilliam

            Comment


            • #7
              That looks like it should work. You might want to make it a little more compact:
              Code:
              if "`c(os)'" == "Windows" {
                  local command type
              }
              else if inlist("`c(os)'", "MacOSX", "Unix") {
                  local command cat
              }
              
              shell `command' `xback' >> `mfilename'.inp
              though I think people might disagree as to which version is more readily understandable.
              Last edited by Clyde Schechter; 20 Jul 2016, 08:41. Reason: Improve formatting of code

              Comment


              • #8
                Thanks. Yes, I went for readability over compactness, mostly in case I need to change the code in the next 10 years.
                -------------------------------------------
                Richard Williams, Notre Dame Dept of Sociology
                StataNow Version: 19.5 MP (2 processor)

                EMAIL: [email protected]
                WWW: https://www3.nd.edu/~rwilliam

                Comment


                • #9
                  Works as I expected on MacOSX. We hope that with the forthcoming rebranding of Mac OS X to macOS, Stata does not follow suit, or lots of OS-specific code will fail on Macs.

                  If I were interested in tight code, I would tighten Clyde's code even further, by recognizing that Windows is the only operating system lacking UNIX underpinnings:

                  Code:
                  local command cat
                  if "`c(os)'" == "Windows" {
                      local command type
                  }
                  
                  shell `command' `xback' >> `mfilename'.inp

                  Comment


                  • #10
                    Thanks William. Yes, I would rather not have OS specific code but I would also rather have some time to do other things today. I'm guessing Windows will be with us for all eternity so maybe something like your code is safest. So I rewrote the code like this:

                    Code:
                        if "`c(os)'" == "Windows" {
                            shell type `xback' >> `mfilename'.inp
                        }
                        else shell cat `xback' >> `mfilename'.inp
                    -------------------------------------------
                    Richard Williams, Notre Dame Dept of Sociology
                    StataNow Version: 19.5 MP (2 processor)

                    EMAIL: [email protected]
                    WWW: https://www3.nd.edu/~rwilliam

                    Comment

                    Working...
                    X