如何保留两个文本文件中的唯一行并丢弃重复项?

发布于 2024-10-20 04:49:26 字数 255 浏览 7 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-10-27 04:49:26

我知道您要求使用 perl 或 C,但在 Unix 中(或使用 MKS 或 Windows 上的等效 Unix 工具包):

sort file1 file2 | uniq -u > file3

没有比这更简单的了。

I know you asked for perl or C, but in Unix (or with MKS or equivalent Unix on Windows toolkit):

sort file1 file2 | uniq -u > file3

It doesn't get much simpler than that.

木有鱼丸 2024-10-27 04:49:26

这里有一些快速的代码可以完成您想要的操作。没有错误检查,并且我假设您的文本文件不会太大,以至于通过将所有文本加载到哈希数组中会耗尽内存。

open(FILE1, "< file1.txt");
open(FILE2, "< file2.txt");

@file1 = <FILE1>;
@file2 = <FILE2>;

foreach $line (@file1, @file2)
{
    chomp($line);
    $TEXT{$line}++;
}

foreach $line (sort keys %TEXT)
{
    if ($TEXT{$line} == 1)
    {
         print $line . "\n";
    }
}

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.

open(FILE1, "< file1.txt");
open(FILE2, "< file2.txt");

@file1 = <FILE1>;
@file2 = <FILE2>;

foreach $line (@file1, @file2)
{
    chomp($line);
    $TEXT{$line}++;
}

foreach $line (sort keys %TEXT)
{
    if ($TEXT{$line} == 1)
    {
         print $line . "\n";
    }
}
囍笑 2024-10-27 04:49:26

计算每一行的数量,然后打印出计数为 1 的行:

#!/usr/bin/perl
use warnings;
use strict;

local @ARGV = ('file.1', 'file.2');
my %lines;
while (<>) {
    $lines{$_}++;
}

print sort grep $lines{$_} == 1, keys %lines;

Count each line, then print out the ones where the count is one:

#!/usr/bin/perl
use warnings;
use strict;

local @ARGV = ('file.1', 'file.2');
my %lines;
while (<>) {
    $lines{$_}++;
}

print sort grep $lines{$_} == 1, keys %lines;
明天过后 2024-10-27 04:49:26

仍然不确定您是否完整地描述了问题。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文