将2个文本文件合并到同一行 [英] Merge 2 text files into one, same lines

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

问题描述

我只有一个文件,其中包含:

I have one file and contains:

file2.txt

PRIMERB
PrinceValiant
Priory
PRISTINA
embossed
heavy
incised
light
Outline
ribbon

file1.txt

PRIMERB 333
PrinceValiant 581
Priory789
PRISTINA3!1
embossed509
heavy5@
incised999
light5*1
Outline937
ribbon-81

我想将这两个文件合并/合并在一起,所以它们就像:

I'd like to combine/merge these two files together so they would be like :

PRIMERB 333 PRIMERB
PrinceValiant 581 PrinceValiant
Priory789 Priory
PRISTINA3!1 PISTINA
embossed509 embossed
heavy5@ heavy
incised999 incised
light5*1 light
Outline937 Outline
ribbon-81 ribbon

我将如何在?

推荐答案

我认为与其复制某种自动化方法,不如直接复制&粘贴...
但这纯粹取决于您在这些文本文件中获得多少行文本.如果它们少于50行,建议您复制(或剪切)并粘贴.
无论如何,我不知道有什么方法可以在Notepad ++中实现自动化.

Instead of finding some way of automating this, I think it'll be easier for you to just copy & paste...
But that purely depends on how many rows of text you got in those text files. If they contain less then 50 lines, I suggest you just copy (or cut) and paste.
I wouldn't know any way to automate that in Notepad++ anyway.

在您请求之后,我编写了一个快速的PHP脚本,该脚本从'file1.txt'和'file2.txt'中提取行并将其组合为'file3.txt'

After your request I wrote a quick PHP script that takes the lines from 'file1.txt' and 'file2.txt' and combines it to 'file3.txt'

<?php
$files1 = file('file1.txt'); // read file1.txt
$files2 = file('file2.txt'); // read file2.txt
// Assuming both files have equal amount of rows.
for($x = 0; $x < count($files1); $x++) {
  $files1[$x] = str_replace(array("\n", "\r"), "", $files1[$x]);
  $files3[$x] = $files1[$x]." ".$files2[$x];
}
$result = implode("", $files3); // combines the array to a single string.
if(file_put_contents('file3.txt', $result)) { // puts the imploded string into file3.txt
  echo "Writing to file 'file3.txt' was successfull.";
}
?>

现在,我想尽最大努力为您提供帮助,但是我目前无法访问自己的域,并且我还没有写任何东西供您将自己的文件上传到该域.

Now I would like to help you best I can, but I cannot access my own domain at this time, and I have not yet wrote something for you to upload your own files to it.

您可以通过下载最新的 USBWebserver

1.从您从USBWebserver网站下载的.zip中提取文件.
2.转到刚刚解压缩的根"文件夹.
3.删除该根"文件夹中的所有内容.
4.复制上面的代码,并将其另存为"root"文件夹中的"index.php"(您也可以使用notepad ++进行此操作).
5.将"file1.txt"和"file2.txt"移动到相同的"root"文件夹.
6.转到一个文件夹并执行'usbwebserver.exe'.
7.弹出窗口时,单击本地主机".
8.如果收到消息:成功写入文件'file3.txt'."您现在应该在该根"文件夹中拥有"file3.txt".

You can run this your own by downloading the latest USBWebserver

1. Extract the files from the .zip you downloaded from the USBWebserver website.
2. Go to the just extracted 'root' folder.
3. Delete everything inside that 'root' folder.
4. Copy the code above and save it as 'index.php' inside the 'root' folder (you can do this with notepad++ too).
5. Move your 'file1.txt' and 'file2.txt' to the same 'root' folder.
6. Go up one folder and execute 'usbwebserver.exe'.
7. Click on 'localhost' when the window pops up.
8. If you get the message: "Writing to file 'file3.txt' was successfull." you should now have 'file3.txt' in that 'root' folder.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆