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

查看:20
本文介绍了将 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("
", "
"), "", $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"移动到同一个根"文件夹中.
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天全站免登陆