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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
NetBackUp_Admin
Posts: 6
Joined: 18 Oct 2009 04:49

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

#1 Post by NetBackUp_Admin » 02 Dec 2009 19:41

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...

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 02 Dec 2009 22:28

*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
Last edited by avery_larry on 10 Dec 2009 12:24, edited 1 time in total.

NetBackUp_Admin
Posts: 6
Joined: 18 Oct 2009 04:49

#3 Post by NetBackUp_Admin » 07 Dec 2009 14:14

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)

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 07 Dec 2009 19:56

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#5 Post by avery_larry » 10 Dec 2009 12:24

I modified the code to show where the rest of your code should go.

Post Reply