Loading ...
Sorry, an error occurred while loading the content.

Merge 2 text files horizontally

Expand Messages
  • batchlover93
    Greetings, I m searching for a script that will be able to merge 2 slightly different text files horizontally for ex. text file 1 test123 test321 test111
    Message 1 of 12 , Jul 15 3:54 AM
    • 0 Attachment
      Greetings,

      I'm searching for a script that will be able to merge 2 slightly different text files horizontally

      for ex.

      text file 1

      test123
      test321
      test111
      test222

      text file 2

      test 123
      test 321
      test 111
      test 222

      The final text file that I want to achieve :

      test123 test 123
      test321 test 321
      test111 test 111
      test222 test 222

      anyone any idea ?

      Thank You !
    • foxidrive
      ... I had this squirreled away for merging four files. Can you edit it? If the files do not have the same number of lines then you need a different script.
      Message 2 of 12 , Jul 15 4:29 AM
      • 0 Attachment
        On 15/07/2011 20:54, batchlover93 wrote:
        > Greetings,
        >
        > I'm searching for a script that will be able to merge 2 slightly different text files horizontally
        >
        > for ex.
        >
        > text file 1
        >
        > test123
        > test321
        > test111
        > test222
        >
        > text file 2
        >
        > test 123
        > test 321
        > test 111
        > test 222
        >
        > The final text file that I want to achieve :
        >
        > test123 test 123
        > test321 test 321
        > test111 test 111
        > test222 test 222
        >
        > anyone any idea ?
        >
        > Thank You !
        >
        >

        I had this squirreled away for merging four files. Can you edit it?

        If the files do not have the same number of lines then you need a different script.


        @echo off
        for /f "tokens=1* delims=:" %%a in ('findstr/n . file1.txt') do (
        for /f "tokens=1* delims=:" %%c in ('findstr/n . file2.txt') do (
        for /f "tokens=1* delims=:" %%e in ('findstr/n . file3.txt') do (
        for /f "tokens=1* delims=:" %%g in ('findstr/n . file4.txt') do (
        if %%a EQU %%c if %%a EQU %%e if %%a EQU %%g (
        >>result.txt echo/%%b=%%d=%%f=%%h)))))
        pause
      • Parag Doke
        ... @if not defined debug echo off rem I believe I picked this debug trick from Rob van der Woude s Scripting Pages. If you need to debug the batch file,
        Message 3 of 12 , Jul 15 4:30 AM
        • 0 Attachment
          My attempt:
          >>>>>
          @if not defined debug echo off
          rem I believe I picked this debug trick from Rob van der Woude's Scripting
          Pages. If you need to debug the batch file, simply set variable debug to say
          1 and run.

          for /f "tokens=1,* delims=:" %%a in ('findstr /n /r "." "C:\text1.txt"') do
          for /f "tokens=1,* delims=:" %%i in ('findstr /n /r "." "C:\text2.txt" ^|
          findstr /r "^%%a:"') do echo %%b %%j >> "C:\text3.txt"

          <<<<<
          There could be far efficient ways, but I just got this to work on:
          text1.txt
          One
          Two
          Three
          Four
          Five

          text2.txt
          Ein
          Zwei
          Drie
          Vier
          Funf

          Hope this helps,
          Parag Doke
          Save paper, save trees. Do not print emails/documents unless absolutely
          necessary.


          On Fri, Jul 15, 2011 at 4:24 PM, batchlover93 <batchlover93@...>wrote:

          > **
          >
          >
          > Greetings,
          >
          > I'm searching for a script that will be able to merge 2 slightly different
          > text files horizontally
          >
          > for ex.
          >
          > text file 1
          >
          > test123
          > test321
          > test111
          > test222
          >
          > text file 2
          >
          > test 123
          > test 321
          > test 111
          > test 222
          >
          > The final text file that I want to achieve :
          >
          > test123 test 123
          > test321 test 321
          > test111 test 111
          > test222 test 222
          >
          > anyone any idea ?
          >
          > Thank You !
          >
          >
          >


          [Non-text portions of this message have been removed]
        • Parag Doke
          The moment I posted my reply, I realized you had shared sample input files ... Sorry for missing that earlier. Tried with your content and worked (or so it
          Message 4 of 12 , Jul 15 4:33 AM
          • 0 Attachment
            The moment I posted my reply, I realized you had shared sample input files
            :-).
            Sorry for missing that earlier. Tried with your content and worked (or so it
            seems).

            Sorry for being dumb,
            Parag Doke
            Save paper, save trees. Do not print emails/documents unless absolutely
            necessary.


            On Fri, Jul 15, 2011 at 5:00 PM, Parag Doke <paragpdoke@...> wrote:

            > My attempt:
            > >>>>>
            > @if not defined debug echo off
            > rem I believe I picked this debug trick from Rob van der Woude's Scripting
            > Pages. If you need to debug the batch file, simply set variable debug to say
            > 1 and run.
            >
            > for /f "tokens=1,* delims=:" %%a in ('findstr /n /r "." "C:\text1.txt"') do
            > for /f "tokens=1,* delims=:" %%i in ('findstr /n /r "." "C:\text2.txt" ^|
            > findstr /r "^%%a:"') do echo %%b %%j >> "C:\text3.txt"
            >
            > <<<<<
            > There could be far efficient ways, but I just got this to work on:
            > text1.txt
            > One
            > Two
            > Three
            > Four
            > Five
            >
            > text2.txt
            > Ein
            > Zwei
            > Drie
            > Vier
            > Funf
            >
            > Hope this helps,
            > Parag Doke
            > Save paper, save trees. Do not print emails/documents unless absolutely
            > necessary.
            >
            >
            >
            > On Fri, Jul 15, 2011 at 4:24 PM, batchlover93 <batchlover93@...>wrote:
            >
            >> **
            >>
            >>
            >> Greetings,
            >>
            >> I'm searching for a script that will be able to merge 2 slightly different
            >> text files horizontally
            >>
            >> for ex.
            >>
            >> text file 1
            >>
            >> test123
            >> test321
            >> test111
            >> test222
            >>
            >> text file 2
            >>
            >> test 123
            >> test 321
            >> test 111
            >> test 222
            >>
            >> The final text file that I want to achieve :
            >>
            >> test123 test 123
            >> test321 test 321
            >> test111 test 111
            >> test222 test 222
            >>
            >> anyone any idea ?
            >>
            >> Thank You !
            >>
            >>
            >>
            >
            >


            [Non-text portions of this message have been removed]
          • batchlover93
            Awesome ! Thanks everyone :) Here s the final code that does exactly what I asked, if it can help someone else... for /f tokens=1* delims=: %%a in
            Message 5 of 12 , Jul 16 11:13 AM
            • 0 Attachment
              Awesome !

              Thanks everyone :)

              Here's the final code that does exactly what I asked, if it can help someone else...

              for /f "tokens=1* delims=:" %%a in ('findstr/n . test.txt') do (
              for /f "tokens=1* delims=:" %%c in ('findstr/n . test2.txt') do (
              if %%a EQU %%c (
              >>test3.txt echo/%%b %%d)))


              --- In batchworld@yahoogroups.com, foxidrive <foxidrive@...> wrote:
              >
              > On 15/07/2011 20:54, batchlover93 wrote:
              > > Greetings,
              > >
              > > I'm searching for a script that will be able to merge 2 slightly different text files horizontally
              > >
              > > for ex.
              > >
              > > text file 1
              > >
              > > test123
              > > test321
              > > test111
              > > test222
              > >
              > > text file 2
              > >
              > > test 123
              > > test 321
              > > test 111
              > > test 222
              > >
              > > The final text file that I want to achieve :
              > >
              > > test123 test 123
              > > test321 test 321
              > > test111 test 111
              > > test222 test 222
              > >
              > > anyone any idea ?
              > >
              > > Thank You !
              > >
              > >
              >
              > I had this squirreled away for merging four files. Can you edit it?
              >
              > If the files do not have the same number of lines then you need a different script.
              >
              >
              > @echo off
              > for /f "tokens=1* delims=:" %%a in ('findstr/n . file1.txt') do (
              > for /f "tokens=1* delims=:" %%c in ('findstr/n . file2.txt') do (
              > for /f "tokens=1* delims=:" %%e in ('findstr/n . file3.txt') do (
              > for /f "tokens=1* delims=:" %%g in ('findstr/n . file4.txt') do (
              > if %%a EQU %%c if %%a EQU %%e if %%a EQU %%g (
              > >>result.txt echo/%%b=%%d=%%f=%%h)))))
              > pause
              >
            • aaciini
              The previous method have the disadvantage of rapidly become slow if the files are large. The old MS-DOS line-oriented EDLIN text editor may be used to achieve
              Message 6 of 12 , Jul 19 12:55 AM
              • 0 Attachment
                The previous method have the disadvantage of rapidly become slow if the files are large. The old MS-DOS line-oriented EDLIN text editor may be used to achieve file editions commanded by a Batch file. As a matter of fact, many people think that this is the reason because such ancient program (and DEBUG) were still included in many Windows versions (at least in XP were) and that is possible to get a running version today (search for it and DEBUG in the web).

                The Batch file shown below allows to horizontally merge any number of files with any number of lines each; the same-numbered lines of each file are merged in the order of the given files.

                HORIZMERGE.BAT:

                @echo off

                REM MERGES SEVERAL FILES HORIZONTALLY
                REM HORIZMERGE basefile part2 part3 ...

                if not exist F3.KEY call :MakeF3Key
                set basefile=%~S1
                if exist EDLIN.IN del EDLIN.IN
                :loop
                shift
                if "%~1" == "" goto exitloop
                for /f "tokens=1* delims=:" %%a in ('findstr /n . "%~1"') do (
                echo %%a
                type F3.KEY
                echo/ %%b
                ) >> EDLIN.IN
                goto loop
                :exitloop
                echo E >> EDLIN.IN
                EDLIN %basefile% < EDLIN.IN > NUL
                del EDLIN.IN
                goto :EOF

                :MakeF3Key
                (
                ECHO E100
                ECHO 0 3D
                ECHO RCX
                ECHO 2
                ECHO W
                ECHO Q
                ) | DEBUG F3.KEY > NUL 2>&1

                Interesting! Isn't it?

                Regards...

                Antonio


                --- In batchworld@yahoogroups.com, "batchlover93" <batchlover93@...> wrote:
                >
                >
                >
                > Awesome !
                >
                > Thanks everyone :)
                >
                > Here's the final code that does exactly what I asked, if it can help someone else...
                >
                > for /f "tokens=1* delims=:" %%a in ('findstr/n . test.txt') do (
                > for /f "tokens=1* delims=:" %%c in ('findstr/n . test2.txt') do (
                > if %%a EQU %%c (
                > >>test3.txt echo/%%b %%d)))
                >
                >
                > --- In batchworld@yahoogroups.com, foxidrive <foxidrive@> wrote:
                > >
                > > On 15/07/2011 20:54, batchlover93 wrote:
                > > > Greetings,
                > > >
                > > > I'm searching for a script that will be able to merge 2 slightly different text files horizontally
                > > >
                > > > for ex.
                > > >
                > > > text file 1
                > > >
                > > > test123
                > > > test321
                > > > test111
                > > > test222
                > > >
                > > > text file 2
                > > >
                > > > test 123
                > > > test 321
                > > > test 111
                > > > test 222
                > > >
                > > > The final text file that I want to achieve :
                > > >
                > > > test123 test 123
                > > > test321 test 321
                > > > test111 test 111
                > > > test222 test 222
                > > >
                > > > anyone any idea ?
                > > >
                > > > Thank You !
                > > >
                > > >
                > >
                > > I had this squirreled away for merging four files. Can you edit it?
                > >
                > > If the files do not have the same number of lines then you need a different script.
                > >
                > >
                > > @echo off
                > > for /f "tokens=1* delims=:" %%a in ('findstr/n . file1.txt') do (
                > > for /f "tokens=1* delims=:" %%c in ('findstr/n . file2.txt') do (
                > > for /f "tokens=1* delims=:" %%e in ('findstr/n . file3.txt') do (
                > > for /f "tokens=1* delims=:" %%g in ('findstr/n . file4.txt') do (
                > > if %%a EQU %%c if %%a EQU %%e if %%a EQU %%g (
                > > >>result.txt echo/%%b=%%d=%%f=%%h)))))
                > > pause
                > >
                >
              • foxidrive
                ... Your code didn t work as pasted into a batch file. d: ADD b file3.txt file.txt file2.txt File not found File not found File not found File not found File
                Message 7 of 12 , Jul 19 1:34 AM
                • 0 Attachment
                  On 19/07/2011 17:55, aaciini wrote:
                  > The previous method have the disadvantage of rapidly become slow if the files are large. The old MS-DOS line-oriented EDLIN text editor may be used to achieve file editions commanded by a Batch file. As a matter of fact, many people think that this is the reason because such ancient program (and DEBUG) were still included in many Windows versions (at least in XP were) and that is possible to get a running version today (search for it and DEBUG in the web).
                  >
                  > The Batch file shown below allows to horizontally merge any number of files with any number of lines each; the same-numbered lines of each file are merged in the order of the given files.
                  >
                  > HORIZMERGE.BAT:
                  >
                  > @echo off
                  >
                  > REM MERGES SEVERAL FILES HORIZONTALLY
                  > REM HORIZMERGE basefile part2 part3 ...
                  >
                  > if not exist F3.KEY call :MakeF3Key
                  > set basefile=%~S1
                  > if exist EDLIN.IN del EDLIN.IN
                  > :loop
                  > shift
                  > if "%~1" == "" goto exitloop
                  > for /f "tokens=1* delims=:" %%a in ('findstr /n . "%~1"') do (
                  > echo %%a
                  > type F3.KEY
                  > echo/ %%b
                  > )>> EDLIN.IN
                  > goto loop
                  > :exitloop
                  > echo E>> EDLIN.IN
                  > EDLIN %basefile%< EDLIN.IN> NUL
                  > del EDLIN.IN
                  > goto :EOF
                  >
                  > :MakeF3Key
                  > (
                  > ECHO E100
                  > ECHO 0 3D
                  > ECHO RCX
                  > ECHO 2
                  > ECHO W
                  > ECHO Q
                  > ) | DEBUG F3.KEY> NUL 2>&1
                  >
                  > Interesting! Isn't it?
                  >
                  > Regards...
                  >
                  > Antonio

                  Your code didn't work as pasted into a batch file.


                  d:\ADD>b file3.txt file.txt file2.txt
                  File not found
                  File not found
                  File not found
                  File not found
                  File not found
                  File not found

                  d:\ADD>dir file*.txt
                  Volume in drive D is D
                  Volume Serial Number is F87A-310C

                  Directory of d:\ADD

                  19/07/2011 18:30 53 file.txt
                  19/07/2011 18:30 53 file2.txt
                  19/07/2011 18:30 1 FILE3.TXT
                • Bushy
                  ... XP were) Unfortunately EDLIN was not included in Windows XP. Its last release was in Windows NT4. DEBUG, however, was and still is.
                  Message 8 of 12 , Jul 19 2:54 PM
                  • 0 Attachment
                    >many people think that this is the reason because such ancient program
                    >(and DEBUG) were still included in many Windows versions (at least in
                    XP were)

                    Unfortunately EDLIN was not included in Windows XP. Its last release was
                    in Windows NT4.
                    DEBUG, however, was and still is.
                  • foxidrive
                    ... XP Pro (at least) has Edlin and so does Windows 7 by the look of it.
                    Message 9 of 12 , Jul 19 7:49 PM
                    • 0 Attachment
                      On 20/07/2011 07:54, Bushy wrote:
                      > >many people think that this is the reason because such ancient program
                      > >(and DEBUG) were still included in many Windows versions (at least in
                      > XP were)
                      >
                      > Unfortunately EDLIN was not included in Windows XP. Its last release was
                      > in Windows NT4.
                      > DEBUG, however, was and still is.


                      XP Pro (at least) has Edlin and so does Windows 7 by the look of it.

                      http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/edlin.mspx?mfr=true


                      http://pcsupport.about.com/od/commandlinereference/tp/windows-7-commands-p1.05.htm
                    • aaciini
                      ... My method works taken a base file and then merge additional parts to it; in other words, the base file must exist. That is: REM HORIZMERGE basefile part2
                      Message 10 of 12 , Jul 19 8:24 PM
                      • 0 Attachment
                        --- In batchworld@yahoogroups.com, foxidrive <foxidrive@...> wrote:
                        >
                        > On 19/07/2011 17:55, aaciini wrote:
                        > > The previous method have the disadvantage of rapidly become slow if the files are large. The old MS-DOS line-oriented EDLIN text editor may be used to achieve file editions commanded by a Batch file. As a matter of fact, many people think that this is the reason because such ancient program (and DEBUG) were still included in many Windows versions (at least in XP were) and that is possible to get a running version today (search for it and DEBUG in the web).
                        > >
                        > > The Batch file shown below allows to horizontally merge any number of files with any number of lines each; the same-numbered lines of each file are merged in the order of the given files.
                        > >
                        > > HORIZMERGE.BAT:
                        > >
                        > > @echo off
                        > >
                        > > REM MERGES SEVERAL FILES HORIZONTALLY
                        > > REM HORIZMERGE basefile part2 part3 ...
                        > >
                        > > if not exist F3.KEY call :MakeF3Key
                        > > set basefile=%~S1
                        > > if exist EDLIN.IN del EDLIN.IN
                        > > :loop
                        > > shift
                        > > if "%~1" == "" goto exitloop
                        > > for /f "tokens=1* delims=:" %%a in ('findstr /n . "%~1"') do (
                        > > echo %%a
                        > > type F3.KEY
                        > > echo/ %%b
                        > > )>> EDLIN.IN
                        > > goto loop
                        > > :exitloop
                        > > echo E>> EDLIN.IN
                        > > EDLIN %basefile%< EDLIN.IN> NUL
                        > > del EDLIN.IN
                        > > goto :EOF
                        > >
                        > > :MakeF3Key
                        > > (
                        > > ECHO E100
                        > > ECHO 0 3D
                        > > ECHO RCX
                        > > ECHO 2
                        > > ECHO W
                        > > ECHO Q
                        > > ) | DEBUG F3.KEY> NUL 2>&1
                        > >
                        > > Interesting! Isn't it?
                        > >
                        > > Regards...
                        > >
                        > > Antonio
                        >
                        > Your code didn't work as pasted into a batch file.
                        >
                        >
                        > d:\ADD>b file3.txt file.txt file2.txt
                        > File not found
                        > File not found
                        > File not found
                        > File not found
                        > File not found
                        > File not found
                        >
                        > d:\ADD>dir file*.txt
                        > Volume in drive D is D
                        > Volume Serial Number is F87A-310C
                        >
                        > Directory of d:\ADD
                        >
                        > 19/07/2011 18:30 53 file.txt
                        > 19/07/2011 18:30 53 file2.txt
                        > 19/07/2011 18:30 1 FILE3.TXT
                        >

                        My method works taken a base file and then merge additional parts to it; in other words, the base file must exist. That is:

                        REM HORIZMERGE basefile part2 part3 ...

                        This is done because the base file is the one that EDLIN edits.
                        If you want the first file be created as the result, after this line:

                        set basefile=%~S1

                        Insert these two:

                        shift
                        copy %1 %basefile% > NUL

                        So:

                        REM HORIZMERGE resultfile part1 part2 part3 ...

                        This way, you may do:

                        b file3.txt file.txt file2.txt

                        Or, if the file is named HORIZMERGE.BAT:

                        horizmerge file3.txt=file.txt,file2.txt

                        Regards...

                        Antonio
                      • aaciini
                        ... C: DOCUME~1 Antonio ... Microsoft Windows XP [Version 5.1.2600] C: DOCUME~1 Antonio ... Starts Edlin, a line-oriented text editor. EDLIN
                        Message 11 of 12 , Jul 19 8:37 PM
                        • 0 Attachment
                          --- In batchworld@yahoogroups.com, Bushy <bushy555@...> wrote:
                          >
                          > >many people think that this is the reason because such ancient program
                          > >(and DEBUG) were still included in many Windows versions (at least in
                          > XP were)
                          >
                          > Unfortunately EDLIN was not included in Windows XP. Its last release was
                          > in Windows NT4.
                          > DEBUG, however, was and still is.
                          >

                          C:\DOCUME~1\Antonio
                          >ver

                          Microsoft Windows XP [Version 5.1.2600]

                          C:\DOCUME~1\Antonio
                          >edlin /?
                          Starts Edlin, a line-oriented text editor.

                          EDLIN [drive:][path]filename [/B]

                          /B Ignores end-of-file (CTRL+Z) characters.

                          C:\DOCUME~1\Antonio
                          >
                        • foxidrive
                          ... Edlin only supports 64kb files, doesn t it? If you have large files then it will simply stop working. Antonio, now that you have clarifed the syntax: I
                          Message 12 of 12 , Jul 19 9:18 PM
                          • 0 Attachment
                            On 19/07/2011 17:55, aaciini wrote:
                            > The previous method have the disadvantage of rapidly become slow if
                            > the files are large. The old MS-DOS line-oriented EDLIN text editor
                            > may be used to achieve file editions commanded by a Batch file. As a
                            > matter of fact, many people think that this is the reason because
                            > such ancient program (and DEBUG) were still included in many Windows
                            > versions (at least in XP were) and that is possible to get a running
                            > version today (search for it and DEBUG in the web).

                            Edlin only supports 64kb files, doesn't it? If you have large files then it will simply stop working.


                            Antonio, now that you have clarifed the syntax:

                            I suspect you were using batch files in the MSDOS days when such kludges were the only way to do a great many things.
                            Then we had Qbasic.exe to rely upon too.


                            I used my two 70 KB files test files (original code takes 2:37 seconds) and your code fails.
                            If you need speed then VBS script takes less than 2 seconds for the same test files.


                            @echo off
                            cscript /nologo abc.vbs > "fileout.txt"


                            'abc.vbs follows


                            Set objFSO = CreateObject("Scripting.FileSystemObject")
                            myFile1="test.txt"
                            myFile2="test2.txt"
                            Dim myFile1Array()
                            Dim myFile2Array()
                            i=0
                            j=0
                            Set objF1 = objFSO.OpenTextFile(myFile1,1)
                            Set objF2 = objFSO.OpenTextFile(myFile2,1)
                            Do Until objF1.AtEndOfLine
                            ReDim Preserve myFile1Array(i)
                            myFile1Array(i) = objF1.ReadLine
                            i=i+1
                            Loop
                            Do Until objF2.AtEndOfLine
                            ReDim Preserve myFile2Array(j)
                            myFile2Array(j) = objF2.ReadLine
                            j=j+1
                            Loop
                            For l=LBound(myFile1Array) To UBound(myFile1Array)
                            WScript.Echo myFile1Array(l) & " " & myFile2Array(l)
                            Next
                            objF1.Close
                            objF2.Close
                            Set objF1 =Nothing
                            Set objF2 = Nothing
                          Your message has been successfully submitted and would be delivered to recipients shortly.