如何保留两个文本文件中的唯一行并丢弃重复项?
我有2个文件。
例如,文件 #1 的内容是:
hi1
hi2
hi4
... 文件 #2 的内容是:
hi1
hi4
hi3
hi5
我想整理这些文档,以便第三个文件只包含:
hi2
hi3
hi5
有人能把我扔到正确的方向吗?我急需!需要 Perl,但也接受 C/C++。
I have 2 files.
For example, the content of file #1 is:
hi1
hi2
hi4
… of file #2 is:
hi1
hi4
hi3
hi5
I would like to sort out these documents so that a third file would contain just:
hi2
hi3
hi5
Can anyone toss me in the right direction? I'm in dire need! Perl is wanted, but C/C++ is accepted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(4)
我知道您要求使用 perl 或 C,但在 Unix 中(或使用 MKS 或 Windows 上的等效 Unix 工具包):
没有比这更简单的了。
I know you asked for perl or C, but in Unix (or with MKS or equivalent Unix on Windows toolkit):
It doesn't get much simpler than that.
这里有一些快速的代码可以完成您想要的操作。没有错误检查,并且我假设您的文本文件不会太大,以至于通过将所有文本加载到哈希数组中会耗尽内存。
Here's a quick bit of code to do what you want. There's no error checking, and I'm assuming that your text files are not so huge that you'll run out of memory by loading all the text into a hash array.
计算每一行的数量,然后打印出计数为 1 的行:
Count each line, then print out the ones where the count is one:
仍然不确定您是否完整地描述了问题。 hi3 不重复,但 hi4 重复。那么输出应该包含 hi3 而不是 hi4 吗?提示:要在 Perl 中检测重复项,您可能需要使用散列。
Still not sure you are describing the problem completely. hi3 is not duplicated, but hi4 is. So should the output contain hi3 instead of hi4? Hint: to detect duplicates in perl, you probably want to use a hash.