将多个文本文件合并为一个

将多个文本文件合并为一个

本文介绍了将多个文本文件合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索到万维网的高低,似乎找不到任何解决方案我的问题!我有多个文本文件,我想结合起来。



显示示例比说明我为什么要做的更容易。



第一个文件如下所示:

  John 
Paul
Mark
Sam
Herold

此文件用作一个主键。



其余的文件包含这样的每个项目的数据。一个程序每小时将这些数据生成一个新文件。

  4 
10
20
5
200

我最熟悉Windows批处理文件,所以我试图写这样的东西:

  for / ftokens = *%% A in(file1.txt)do 
(for / ftokens = *%% B in(file2.txt)do(echo %% A,%% B>> combined.txt))
pre>

不幸的是,每个人都写下每一个价值。如果这样会按预期工作,最终的结果将是这样的:

  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 
保罗,10,5,6,1 ,7,9,34,56,1,76,48,23,222,12,54,67,23,652,1,6,71,3,6,4

等等。



我使用的软件以这种格式显示数据,不能更改。我可以接受任何建议。

解决方案

您可以在批处理中阅读几个输入文件程序通过标准手柄。记住0是Stdin,1是Stdout,2是Stderr,但是这样可以使用3到9可用!下面的批处理文件使用两个文件的内容进行文件合并当然最多可以使用8个文件。

  @echo off 
setlocal EnableDelayedExpansion
Rem使用FOR / F命令读取第一个文件
Rem通过标准句柄读取第二个文件3
3< file2.txt(for / Fdelims =%% a in(file1.txt)do(
Rem从file2.txt中读取下一行
set / P line2 =<& 3
Rem两个文件的回波线
echo %% a,!line2!
))

更多详情请见:


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.

解决方案

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

这篇关于将多个文本文件合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:34