gpt4 book ai didi

linux - 标记两个文件中的差异

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:45 24 4
gpt4 key购买 nike

我知道比较 2 个文件是一个典型的问题,并且有很多关于这个问题的讨论。但是我在处理文本文件时遇到了一个完全不同的问题:我有两个文本文件,它们的行数可能不同。现在我想比较两个文件并找到不同的行。之后我想标记两个文件中的所有差异。例如这里是我的文件的内容:

文件1.txt:

This is the first line.
This line is just appeared in File1.txt.
you can see this line in both files.
this line is also appeared in both files.
this line and,
this one are mereged in File2.txt.

文件2.txt:

This is the first line.
you can see this line in both files.
this line is also appeared in both files.
this line and, this one are mereged in File2.txt.

处理后我希望两个文件都像这样:

文件1.txt:

This is the first line.
<Diff>This line is just appeared in File1.txt.</Diff>
you can see this line in both files.
this line is also appeared in both files.
<Diff>this line and,</Diff>
<Diff>this one are merged in File2.txt.</Diff>

文件2.txt:

This is the first line.
<Diff></Diff>
you can see this line in both files.
this line is also appeared in both files.
<Diff>this line and, this one are mereged in File2.txt.</Diff>
<Diff></Diff>

我该怎么做?我知道某些工具(例如 diff)可以帮助我,但我如何才能将它们的结果转换为这种格式?

提前谢谢你。

最佳答案

您可以使用 Algorithm::Diff。这是一个几乎可以像您想要的那样产生输出的示例,也许您可​​以调整它以获得您想要的确切输出:

use Algorithm::Diff;
my $diff = Algorithm::Diff->new( \@seq1, \@seq2 );

my @out1;
my @out2;

while( $diff->Next() ) {
if ($diff->Same) {
push @out1, $diff->Items(1);
push @out2, $diff->Items(2);
}
elsif (not $diff->Items(2) ) {
for ($diff->Items(1)) {
chomp;
push @out1, "<Diff>$_</Diff>\n";
}
push @out2, "<Diff></Diff>\n";
}
elsif (not $diff->Items(1)) {
for ($diff->Items(2)) {
chomp;
push @out2, "<Diff>$_</Diff>\n";
}
push @out1, "<Diff></Diff>\n";
}
else {
for ($diff->Items(1)) {
chomp;
push @out1, "<Diff>$_</Diff>\n";
}
for ($diff->Items(2)) {
chomp;
push @out2, "<Diff>$_</Diff>\n";
}
}
}

输出:

@out1:
This is the first line.
<Diff>This line is just appeared in File1.txt.</Diff>
you can see this line in both files.
this line is also appeared in both files.
<Diff>this line and,</Diff>
<Diff>this one are mereged in File2.txt.</Diff>


@out2:
This is the first line.
<Diff></Diff>
you can see this line in both files.
this line is also appeared in both files.
<Diff>this line and, this one are mereged in File2.txt.</Diff>

关于linux - 标记两个文件中的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11070174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com