Computing.Net > Forums > Programming > Merge txt files without appending

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Merge txt files without appending

Reply to Message Icon

Name: Shr0Om
Date: October 16, 2005 at 12:03:43 Pacific
OS: Win Xp
CPU/Ram: Amd 64
Comment:

Hi.. Im working on a "auto rapport" script atm, but im stuck at one point. Lets say i have 2 txt files i want to merge, but not append. Example, file1.txt contains:
1
2
3
And file2.txt contains:
A
B
C

I want to merge those files so file3.txt looks like this:
1A
2B
3C

Is this possible?? If its not possible doing in a batch, does anyone knows of a
program that can do this. (A program that can be runned in cmd).
Hope anyone can help:)



Sponsored Link
Ads by Google

Response Number 1
Name: Shr0Om
Date: October 16, 2005 at 14:41:12 Pacific
Reply:

Aw, i completely forgot to mention its a batch script im writing..


0

Response Number 2
Name: marklunda
Date: October 18, 2005 at 06:48:25 Pacific
Reply:

check out "join" and "paste" commands in UNIX.


0

Response Number 3
Name: Mechanix2Go
Date: October 18, 2005 at 06:53:18 Pacific
Reply:

Hi S,

You got me on this one.

I hope IVO or another power player can get it.


If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: IVO
Date: October 18, 2005 at 08:30:39 Pacific
Reply:

Sorry, but as far as I know there is no way to achieve that target by batch scripting under MS-Windows. A batch script can't read a line from a text file, access another file to join the records and then resume the process untill the EOF, as once opened a file must be browsed without break.

Obviously the job can be done however, but the tricky code required is highly inefficient and cumbersome.

So better you write a straightforward utility in whatever language you like.

To M2, why did you wipe out your e-mail address in your recent posts?


0

Response Number 5
Name: wizard-fred
Date: October 18, 2005 at 08:54:11 Pacific
Reply:

As mentioned bt IVO there is a problem of a batch handling multiple file io. The solution is easy in almost any programming language provided there is no need for data verication or error handling.
Open Input File 1, Input File 2, Output File
Do until end of input
read inputs
write output
repeat


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: October 18, 2005 at 09:01:27 Pacific
Reply:

Hi IVO,

I didn't notice it was gone.

Thanks


If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: Shr0Om
Date: October 18, 2005 at 11:08:39 Pacific
Reply:

I was afraid so.. Ye, i might have a "bad idea" on how it "maybe" could be done, but it would be tricky&higly inefficient.. And it probably wouldn't work anyway:P
Unfortunately i dont know any programming languages. (i knew basic java many years ago, but its all lost). But, do any of you know of any command based programs that can do this?
I wonder if i should look into php scripting, as i have a feeling it would be more suitable..

Kristian: Im on windows, not linux unfortunately.


0

Response Number 8
Name: FishMonger
Date: October 18, 2005 at 13:31:25 Pacific
Reply:

It can be done in php but that would be my 4th or 5th choice. The best scripting language for this task is Perl.


0

Response Number 9
Name: Shr0Om
Date: October 18, 2005 at 13:56:01 Pacific
Reply:

I managed to find a solution to my problem. I was not able to merge the txt files, but i found another way of getting the result as i wanted it. Its ineffective programming, but the script doesn't use many seconds to complete the task anyway.

I wont go into the depths of explaining how i did it, as i think i would have to explain the whole purpose of the script first, which is a bit complicated.

Basically, i collected data from file1 in variables (A,B,C,D,E,F,G) and echo'ed A;B;C;D;E;F;G >> Report.csv
Then collected data from file2 in the same way and echo'ed A;B;C;D;E;F;G >> Report.csv

The trick i did was to only retrive data from 1file at the time (which i didnt before).

Ok, if you have no idea what im talking about, i fully understand it.

I will paste the source here, in case someone are really interested in what the script did&how i made it work(its not fully finished yet tho).
Now, please NOTE that its POOR programming.. More or less extreme programming, so the variable names&documentation are useless (more or less)..

The purpose of the script was to pick out interesting data from numerous reports, then output it in one semicolon delimited file.


@Echo Off
SetLocal EnableDelayedExpansion
color 1e

set CurDir=%cd%

:Continue
cls
Echo Working.. Please wait
echo [-* ]
::Make a list of files that script should retrive data from
cd INPUT-DATA
dir /p *.txt >>list.tmp

cls
Echo Working.. Please wait
echo [---* ]
::Filter out some junk
findstr "sd0134ly.ls1" list.tmp >>tmp.txt
del list.tmp

cls
Echo Working.. Please wait
echo [------* ]

::Another junk filter. New list will now contain file names only

For /F "tokens=1-5* delims= " %%A in (tmp.txt) Do (
Set TxtLine=%%A %%B %%C %%D
Echo %%D %%E>>FINISHED.txt)
del tmp.txt
cd..

set count=1
copy "%CurDir%\INPUT-DATA\FINISHED.txt"
:start
set /p File=<FINISHED.txt
copy "%CurDir%\INPUT-DATA\%File%"
if exist File2Process.txt del File2Process.txt
echo %File% >>File2Process.txt

::Removing the filename ontop of list in FINISHED.txt

findstr /v %File% FINISHED.txt >>tmp.txt
del FINISHED.txt
ren tmp.txt FINISHED.txt
set /a count=count+1


cls
Echo Working.. Please wait
echo [--------* ]


::Collecting interesting data
For /F %%i in (File2Process.txt) Do find "Ant bestilte linier" %%i >>01.txt
For /F %%i in (File2Process.txt) Do find "Ant linier i underdækning" %%i >>01.txt
For /F %%i in (File2Process.txt) Do find "Kundeservicegrad" %%i >>01.txt
For /F %%i in (File2Process.txt) Do find "Bestilt værdi" %%i >>01.txt
For /F %%i in (File2Process.txt) Do find "Saml ant ordrer" %%i >>01.txt
For /F %%i in (File2Process.txt) Do find "Gensnitsværdi" %%i >>01.txt
For /F %%i in (File2Process.txt) Do find "Gensnit ant linier" %%i >>01.txt


cls
Echo Working.. Please wait
echo [----------* ]

::Filtering out some junk
findstr /v "SD0134" 01.txt >>tmp.txt


::Collecting the lines that are interesting
::The liste1-7 files contains the search string
findstr /g:"%CurDir%\DK\liste1.txt" tmp.txt >>1oktmp.txt
findstr /g:"%CurDir%\DK\liste2.txt" tmp.txt >>2oktmp.txt
findstr /g:"%CurDir%\DK\liste3.txt" tmp.txt >>3oktmp.txt
findstr /g:"%CurDir%\DK\liste4.txt" tmp.txt >>4oktmp.txt
findstr /g:"%CurDir%\DK\liste5.txt" tmp.txt >>5oktmp.txt
findstr /g:"%CurDir%\DK\liste6.txt" tmp.txt >>6oktmp.txt
findstr /g:"%CurDir%\DK\liste7.txt" tmp.txt >>7oktmp.txt


cls
Echo Working.. Please wait
echo [------------* ]

For /F "tokens=1-4* delims= " %%A in (1oktmp.txt) Do (
Set TxtLine=%%A %%B %%C %%D
Echo %%D >>1ok.txt)


For /F "tokens=1-5* delims= " %%A in (2oktmp.txt) Do (
Set TxtLine=%%A %%B %%C %%D %%E
Echo %%E >>2ok.txt)


For /F "tokens=1-2* delims= " %%A in (3oktmp.txt) Do (
Set TxtLine=%%A %%B
Echo %%B >>3ok.txt)


For /F "tokens=1-3* delims= " %%A in (4oktmp.txt) Do (
Set TxtLine=%%A %%B %%C
Echo %%C >>4ok.txt)


For /F "tokens=1-4* delims= " %%A in (5oktmp.txt) Do (
Set TxtLine=%%A %%B %%C %%D
Echo %%D >>5ok.txt)


For /F "tokens=1-2* delims= " %%A in (6oktmp.txt) Do (
Set TxtLine=%%A %%B
Echo %%B >>6ok.txt)


For /F "tokens=1-4* delims= " %%A in (7oktmp.txt) Do (
Set TxtLine=%%A %%B %%C %%D
Echo %%D >>7ok.txt)


cls
Echo Working.. Please wait
echo [--------------* ]

set /p FileInputA=<5ok.txt
set /p FileInputB=<1ok.txt
set /p FileInputC=<7ok.txt
set /p FileInputD=<2ok.txt
set /p FileInputE=<3ok.txt
set /p FileInputF=<4ok.txt
set /p FileInputG=<6ok.txt
Echo Line %count%;%FileInputA%;%FileInputB%;%FileInputC%;%FileInputD%;%FileInputE%;%FileInputF%;%FileInputG% >> ManagmentReport.csv

cls
Echo Working.. Please wait
echo [----------------*]


del tmp.txt
del 01.txt
del 1oktmp.txt
del 2oktmp.txt
del 3oktmp.txt
del 4oktmp.txt
del 5oktmp.txt
del 6oktmp.txt
del 7oktmp.txt
del 1ok.txt
del 2ok.txt
del 3ok.txt
del 4ok.txt
del 5ok.txt
del 6ok.txt
del 7ok.txt

if %count% EQU 12 (goto exit) else goto start

EndLocal


0

Response Number 10
Name: dtech10
Date: October 18, 2005 at 15:59:09 Pacific
Reply:

Hi
You could use something like this.
Would'nt be very fast or eficient
on large files.
-----
@echo off
rem This produces OneLine.com
echo N ONELINE.COM > OneLine.scr
echo A 100 >> OneLine.scr
echo MOV AH,2 >> Oneline.scr
echo MOV SI,82 >> Oneline.scr
echo MOV,DL,[SI] >> Oneline.scr
echo CMP DL,D >> Oneline.scr
echo JZ 113 >> Oneline.scr
echo MOV AH,2 >> Oneline.scr
echo INT 21 >> Oneline.scr
echo INC SI >> Oneline.scr
echo JMP 105 >> Oneline.scr
echo MOV AH,4C >> Oneline.scr
echo INT 21 >> Oneline.scr
echo. >> Oneline.scr
echo R CX >> Oneline.scr
echo 17 >> Oneline.scr
echo W >> Oneline.scr
echo Q >> Oneline.scr
debug < OneLine.scr
del OneLine.scr
----

------
@echo off
setlocal EnableDelayedExpansion
set Num=1
find /v /n "" File1.txt > Test1.txt
find /v /n "" File2.txt > Test2.txt
type nul > File3.txt
for /f "tokens=1-2 delims=[]" %%a in (Test1.txt) do (
if %%a==!Num! (
if not "%%b"=="" oneline %%b>> File3.txt
for /f "tokens=1-3 delims=[]" %%c in (Test2.txt) do (
if %%c==!Num! (
if not "%%d"=="" echo %%d>> File3.txt
)
)
set /a Num=!Num!+1
)
)

del Test?.txt
set Num=
-----------

Hi
You could use something like this.
Would'nt be very fast or eficient
on large files.
-----
@echo off
rem This produces OneLine.com
echo N ONELINE.COM > OneLine.scr
echo A 100 >> OneLine.scr
echo MOV AH,2 >> Oneline.scr
echo MOV SI,82 >> Oneline.scr
echo MOV,DL,[SI] >> Oneline.scr
echo CMP DL,D >> Oneline.scr
echo JZ 113 >> Oneline.scr
echo MOV AH,2 >> Oneline.scr
echo INT 21 >> Oneline.scr
echo INC SI >> Oneline.scr
echo JMP 105 >> Oneline.scr
echo MOV AH,4C >> Oneline.scr
echo INT 21 >> Oneline.scr
echo. >> Oneline.scr
echo R CX >> Oneline.scr
echo 17 >> Oneline.scr
echo W >> Oneline.scr
echo Q >> Oneline.scr
debug < OneLine.scr
del OneLine.scr
----

------
@echo off
setlocal EnableDelayedExpansion

set Num=1

find /v /n "" File1.txt > Test1.txt
find /v /n "" File2.txt > Test2.txt

type nul > File3.txt

for /f "tokens=1-2 delims=[]" %%a in (Test1.txt) do (
if %%a==!Num! (
if not "%%b"=="" oneline %%b>> File3.txt
for /f "tokens=1-3 delims=[]" %%c in (Test2.txt) do (
if %%c==!Num! (
if not "%%d"=="" echo %%d>> File3.txt
)
)
set /a Num=!Num!+1
)
)

del Test?.txt
set Num=
-----------



0

Response Number 11
Name: Mechanix2Go
Date: October 18, 2005 at 16:08:57 Pacific
Reply:

That's heavy, brother.

How long you been into that?


If at first you don't succeed, you're about average.

M2


0

Response Number 12
Name: FishMonger
Date: October 18, 2005 at 18:07:14 Pacific
Reply:

Here's a Perl solution. This assumes file 2 has the same or more lines that file1. If the files are large, we'd want to make a couple adjustments and loop through the files instead of slurping them into arrays.

#!perl -w

use strict;

my $file1 = 'file1.txt';
my $file2 = 'file2.txt';
my $i;

open F1, $file1 or die $!;
chomp(my @file1 = <F1>);
close F1;

open F2, $file2 or die $!;
my @file2 = <F2>;
close F2;

open F3, '>file3.txt' or die $!;
for $i (0..$#file1) {
print F3 $file1[$i];
print $file2[$i];
}

if ($#file2 > $i) { print F3 $file2[$_] for ($file2[$i]..$#file2); }

close F1;
close F2;
close F3;


0

Response Number 13
Name: Shr0Om
Date: October 19, 2005 at 06:38:22 Pacific
Reply:

dtech10: I didnt quite understand your code, heh..

Mechanix2Go: If you refered the question to me, ive been at it for couple of days&im still working on it:) On the bright side i get paid for it although scripting is not what i do in my job.

And finally,
FishMonger: Perl scripting looked very interesting. I think i'll have a look at it in the near future:)

Thnx for the tips ya'll :D


0

Response Number 14
Name: dtech10
Date: October 19, 2005 at 12:49:33 Pacific
Reply:

Hi
Perl would be a better solution.
batch Files were never designed for anything complex.
The first batch file when run creates a OneLine.com file.
This behaves like the 'Echo' command but doe's not send
a Cr/LF after it, so next echo command will be on the same line.
ie.
@echo off
oneline.com Comp> tmp.txt
echo uter >> tmp.txt
tmp.txt will now contain Computer

-----------
@echo off
rem This enables you to use variable expansion
rem in for loops
setlocal EnableDelayedExpansion
rem set env var to 1
set Num=1
rem add numbers to file1&2 in Test1&2
find /v /n "" File1.txt > Test1.txt
find /v /n "" File2.txt > Test2.txt
rem Create a empty file
type nul > File3.txt
rem this cycle's through the two line numbered files
rem looking for the same line numbers
for /f "skip=2 tokens=1-2 delims=[]" %%a in (Test1.txt) do (
if %%a==!Num! (
rem Dont echo blank lines and
rem echo without CR/LF (oneline.com)
if not "%%b"=="" oneline %%b>> File3.txt
for /f "tokens=1-3 delims=[]" %%c in (Test2.txt) do (
if %%c==!Num! (
if not "%%d"=="" echo %%d>> File3.txt
)
)
rem Increment Num
set /a Num=!Num!+1
)
)

del Test?.txt
set Num=


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Merge txt files without appending

batch file to clear txt file www.computing.net/answers/programming/batch-file-to-clear-txt-file/12925.html

Merging text files using batch file www.computing.net/answers/programming/merging-text-files-using-batch-file/19906.html

Appending txt files in VB www.computing.net/answers/programming/appending-txt-files-in-vb/9577.html