4

I have searched the world wide web high and low, and can't seem to find any solution for my problem! I have multiple text files that I would like to combine.

It's easier to show examples than to explain exactly why I'm trying to do.

The first file looks like this:

John
Paul
Mark
Sam
Herold

This file serves as a "primary key".

The rest of the files contain data for each item like this. A program generates this data into a new file each hour.

4
10
20
5
200

I'm most familiar with windows batch files, so I tried to write something like this:

for /f "tokens=*" %%A in (file1.txt) do 
    (for /f "tokens=*" %%B in (file2.txt) do (echo %%A,%%B>>combined.txt))

Unfortunately that writes every value to every person. If this would be working as expected, the end result would be like this:

John,4,2,6,9,1,2,5,6,12,51,53,3,6,7,8,1,4,7,2,743,21,4,7,5
Paul,10,5,6,1,7,9,34,56,1,76,48,23,222,12,54,67,23,652,1,6,71,3,6,4

and so on.

The software I am using presents the data in this format, and cannot be changed. I am open to any and all suggestions.

Mofi
  • 46,139
  • 17
  • 80
  • 143
bonneyt
  • 99
  • 1
  • 3
  • 9

3 Answers3

14

You may read several input files in a Batch program via the standard handles. Remember that 0 is Stdin, 1 is Stdout and 2 is Stderr, but this leaves handles 3 to 9 available! The Batch file below do a file merge with the contents of two files; of course, up to 8 files can be combined with this method.

@echo off
setlocal EnableDelayedExpansion
Rem First file is read with FOR /F command
Rem Second file is read via standard handle 3
3< file2.txt (for /F "delims=" %%a in (file1.txt) do (
   Rem Read next line from file2.txt
   set /P line2=<&3
   Rem Echo lines of both files
   echo %%a,!line2!
))

Further details here: http://www.dostips.com/forum/viewtopic.php?f=3&t=3126

Aacini
  • 65,180
  • 12
  • 72
  • 108
1

The unix command paste maybe what you're looking for. Also see this question on StackOverflow.

Community
  • 1
  • 1
Jannik Jochem
  • 1,516
  • 1
  • 14
  • 28
1

You could just do:

paste -d, file*.txt > combined.txt

if you have paste available. You may need to install cygwin, or work on a *nix machine. (You did say you are open to all suggestions!) This relies on the data files being named sequentially. If you want to tweak the order, you can spell it out instead of using the glob.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Thanks for the suggestion! The file names are Dump.01 through Dump.24, but I can always manipulate them to being Dump1.txt through Dump24.txt. I'll give this a try today! – bonneyt Jan 25 '13 at 12:36
  • As you suggested, Paste did the job. When opening the combined text file in notepad, it looks perfect. when opening the same text file in excel and try to delimit on comma, it is all out of order. any suggestions? – bonneyt Jan 25 '13 at 13:31
  • My problem with paste wasn't paste itself. some of the text files contained extra spaces that excel was picking up as a tab delimiter. To remove the spaces, I ran them through a for loop: for /f "delims=" %%A in ('type file1.txt') do (echo %%A>>newfile1.txt). I'm not sure why this works, but it does! – bonneyt Jan 25 '13 at 17:46