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.

4
  • the second files are named based on the usernames of the first file ? Jan 25, 2013 at 12:32
  • The data corresponds to the names in the first file
    – bonneyt
    Jan 25, 2013 at 12:35
  • So shouldn't the first number for Paul be 10, not 3?
    – Bali C
    Jan 25, 2013 at 12:36
  • yeah sorry, I was typing these in as an example. This isn't actual data
    – bonneyt
    Jan 25, 2013 at 12:46

3 Answers 3

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

3
  • Perhaps a simpler approach would be to read each file into an array quasi-array of variables and then loop over them. Sep 12, 2023 at 13:54
  • Perhaps...You would need to insert one loop for each input file, and then one loop more for the final merge output (simpler?). Besides, if the files are large, the process would slow down because the amount of environment variables. In this same site there is a (better) reference about arrays
    – Aacini
    Sep 12, 2023 at 19:02
  • Yes, it is conceptually more straigtforward (in that it does not rely on clever tricks), and therefore simpler. Yes, it is unsuitable for very large files. Thanks for the answer about arrays! Sep 13, 2023 at 8:40
1

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

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

3
  • 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, 2013 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, 2013 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, 2013 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.