merge two txt files

January 2, 2010 at 23:38:14
Specs: Windows XP
hello,
I want to create a new txt file by merging content of two different txt files.As for example, I have two txt files test1.txt and test2.txt.
The content of test1.txt is
3
3
4
5
3

and the content of test2.txt is
45.6 67.7 89.9
44.6 66.7 88.9
43.6 65.7 87.9
42.6 64.7 86.9
41.6 63.7 85.9

Now I want a txt named text3.txt by merging test1.txt and test2.txt such that the content of test3.txt be
3 45.6 67.7 89.9
3 44.6 66.7 88.9
4 43.6 65.7 87.9
5 42.6 64.7 86.9
3 41.6 63.7 85.9

how to do this using batch??
thank you


See More: merge two txt files

Report •


#1
January 3, 2010 at 03:43:47
What kind of merge are we talking about?


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 3, 2010 at 14:30:12
Looks like line 1 of file 1 with line 1 of file 2,
line 2 of file 2 with line 2 of file 2,
etc. etc.

?


Report •

#3
January 3, 2010 at 20:19:36
yes that kind og merge.how cna i do that?

Also my file size is very big about 150589 kb of data.

I urgently need the solution.

thank you


Report •

Related Solutions

#4
January 3, 2010 at 22:04:03
if you're set on using batch, this might work, but it's very inefficient (batch can't handle parallel file channels, i don't think. If it can someone let me know how!!) If this is intended for "industrial-standards" application, i would definitely suggest another platform (even QB can EASILY do this type of op!) This requires re-opening and skipping N lines of the secondary file for EACH line of the primary!

@echo off & setlocal enabledelayedexpansion
set /a nn=0
for /f "tokens=* delims=" %%a in (fil1) do (
set out=%%a
set /a nn+=1
set /a n2=0
call :bb
)
goto :eof

:bb
for /f "tokens=* delims=" %%b in (fil2) do (
set /a n2+=1
if !n2! equ !nn! echo !out! %%b>>fil3 & goto :eof
)

good luck.

ps: QB version also easy trans to vbscript or
vbasic:
OPEN "FILE1" FOR INPUT AS #1
OPEN "FILE2" FOR INPUT AS #2
OPEN "OUTP" FOR OUTPUT AS #3
DO WHILE NOT EOF(1)
LINE INPUT #1,K$
LINE INPUT #2,B$
PRINT #3,K$+" "+B$
LOOP
CLOSE


Report •

#5
January 3, 2010 at 23:30:58

thank you nbrane for ur help

Report •

#6
January 3, 2010 at 23:48:33
This works with the small sample files but will likely bomb with very large ones.

==============================

@echo off > newfile & setLocal EnableDELAYedeXpansion

set N=

for /f "tokens=* delims= " %%a in (one) do (
  set /a N+=1
  set v!N!=%%a
)

set N=

for /f "tokens=* delims= " %%i in (two) do (
  set /a N+=1
  set S=%%i
  call :sub1 !N!
)

goto :eof

:sub1
  >> newfile echo !v%1! !S!
goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question