Batch merge two txt files

May 7, 2009 at 07:44:48
Specs: Windows XP, 2 gb
My problem: I have two files data1.txt and data2. txt that I need to merge in a single file datanew.txt

data1.txt has the following simplified structure

A B C
1 2 3
11 22 33
111 222 333

data2.txt has the following simplified structure

D
4
44
444

datatnew.txt the final output should look like:

A B C D
1 2 3 4
11 22 33 44
111 222 333 444

My rude attempt is (sorry for this)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@echo off & setlocal

set /P line1data1=< data1.txt
set /p line1data2=< data2.txt

echo.%line1data1% %line1data2%> data_new.txt

for /F %%a "skip=1 tokens=*" in (data2.txt) do set d=%%a

for /F "skip=1 tokens=*" %%j in (data1.txt) do (
echo.%%j %%d >> data_new.txt
)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

the two "for" statements are clearly not working at all…
Any hint to go forward?
Sorry but it’s long time I’m not practising batch scripting and I definitely “lost the hand”
Thank you for your support

max


See More: Batch merge two txt files

Report •


#1
May 7, 2009 at 08:58:59
@echo off & setlocal EnableDelayedExpansion > Data.tmp
set N=0
for /F "delims=" %%a in (Data1.txt) do (
  set /A N+=1
  set Tag=!N!
  if !N! lss   10 set Tag=0!Tag!
  if !N! lss  100 set Tag=0!Tag!
  if !N! lss 1000 set Tag=0!Tag!
  echo.!Tag!A%%a>> Data.tmp
)
set N=0
for /F "delims=" %%a in (Data2.txt) do (
  set /A N+=1
  set Tag=!N!
  if !N! lss   10 set Tag=0!Tag!
  if !N! lss  100 set Tag=0!Tag!
  if !N! lss 1000 set Tag=0!Tag!
  echo.!Tag!B%%a>> Data.tmp
)
type nul > DataNew.txt
set FlipFlop=OFF
for /F "delims=" %%a in ('sort ^< Data.tmp') do (
  if !FlipFlop!==OFF (
    set rowA=%%a
    set rowA=!rowA:~5!
    set FlipFlop=ON
  ) else (
    set rowB=%%a
    set rowB=!rowB:~5!
    set FlipFlop=OFF
    echo.!rowA! !rowB!>> DataNew.txt
  )
)
del Data.tmp
:: End_Of_Batch


Report •

#2
May 7, 2009 at 17:26:09
the "standard" tool to use for merging files like that is paste. here's a windows version if you can download and use it.

c:\test> paste file1 file2 > newfile


Report •

#3
May 8, 2009 at 00:14:40
To ivo

what else to say? It’s simply perfect and neat!
the filp-flop trick is a really masterpiece: after carefully studying it I think I understand the general ‘architecture’ of your code even if I’m still missing some tiny details (by the way there seems to be no tiny details at all in your great style of programming!)
Anyhow:
What is the use of the “^” within the statement “for /f….in (‘sort ^< Data.tmp’)….”

…and then I’m always in trouble with the use of dynamic variables declared with the “ ! ! “, but that it is definitely just a my problem…

Thank you so much for your help; you are always giving me a lot of brain food


To ghostdog

thank you so much for your advice; I did not know about the wealth of core utilities for windows you pointed me out; I think I’ll make great use of it….


ciao to all you

max


Report •

Related Solutions

#4
May 8, 2009 at 00:33:04
hi gostdog

another question about the use of coreutils:
after the installation of the package should I put all the *.exe under the dir C:\WINNT\system32
in order to use it anywhere?

thanks

max


Report •

#5
May 8, 2009 at 00:59:21
its either you do that, or you put them in a specific folder, then include that folder in your PATH variable. I am sure you know how to do that so i will not elaborate.

Report •

#6
May 8, 2009 at 01:09:50
ok, I've done it: it works perfectly!
thank you again
max

Report •

#7
May 8, 2009 at 05:40:55
Thanks maxbre for your words,about your questions

- The caret symbol (^) must prefix pipes and redirectors in the For clause to allow the interpreter correctly parses the coded command.

- The full explanation about dynamic variables would require a long tutorial; as a practical rule enable Delayed Expansion every time you need to access environment variables defined or modified inside the same block, i.e. a sequence of statements embraced by () and then mark every variable by using !.


Report •


Ask Question