Page 1 of 1

How to... merge lines?????

Posted: 02 Dec 2009 19:41
by NetBackUp_Admin
Hi there,

I am still pretty new to batch scripting world, been trying different ways to do this without good results... Here I go:

Let's say that I have 2 files, both of them with multiple colums each and that will have always the same number of lines. What i need is to merge the contents of each line in these files into a third one.

Something like this:

file1.txt

COL_1 COL_2 COL_3 COL_4
Blah Blah Blahblah WhatEver
A E I O
data data2 dataW pl




file2.txt

COL_A COL_B COL_C
TA TE TI
DO RE MI
just in time





What i need is to get something like this:

file3.txt

COL_1 COL_2 COL_3 COL_4 COL_A COL_B COL_C
Blah Blah Blahblah WhatEver TA TE TI
A E I O DO RE MI
data data2 dataW pl just in time


That's all... any hapl will be highly appreciated!!! :roll:


EDIT: I had tab spaces between columns... but was lost when pasting here... i guess wou will be able to figure it out anyway...

Posted: 02 Dec 2009 22:28
by avery_larry
*UNTESTED*

Code: Select all

@echo off
setlocal enabledelayedexpansion
set idx=0

for /f "delims=" %%a in (file1.txt) do (
   set /a idx+=1
   set "line!idx!=%%a"
)

set idx=0
for /f "delims=" %%a in (file2.txt) do (
   set /a idx+=1
   call :process "%%a"
)
echo The rest of your code goes here, in front of the (final)
echo goto :eof, which is needed or the subroutine :process will
echo run again at the end of this code.
goto :eof

:process
echo.!line%idx%! %~1>>file3.txt
goto :eof

Posted: 07 Dec 2009 14:14
by NetBackUp_Admin
Thank you man! This worrked just great.

Only that I would like the final file to be displayed at the end of the batch.

I tried adding:

type file3.txt

nothing...

:eof
type file3.txt

nothing...

for /f seems to llop in circles ignoring whatever is after the second "goto :eof" and at the end of the batch i get (on screen) "goto :eof" echoed twice.

Is there a way to show the result file on screen once it has been created?

I would also like (if possible) to be able to add more code... (IE: I have an email dispatcher vbs script that i call right at the end of my scripts so i get the output file in my inbox).

Again, thank you for all your help! (Actually, 2nd time u make my life easyler :D)

Posted: 07 Dec 2009 19:56
by DosItHelp
NetBackUp_Admin,

Add more code as you wish right before the first goto:eof.

The first goto:eof in avery_larry's code terminates the main part of the batch script, everything you add afterwarts will not directly execute.
The second goto:eof you see is the terminator of the :process function he added at the end. The :process function is called from the main part of the batch script.

Hope this helps.

Posted: 10 Dec 2009 12:24
by avery_larry
I modified the code to show where the rest of your code should go.