我很好奇是否有一种方法可以仅通过 diff 获取更改的行,而不是新添加的行?
我的意思是,假设我有两个文件 file1 和 file2。
文件1是:
abc=123
def=234
klm=10.10
xyz=6666
文件2是:
abc+=123
def=234
klm=10.101
xyz=666
stackoverflow=1000
superuser=2000
wtf=911
我想要的是给出像 diff <parameters> file1 file2
这样的命令并得到类似的输出
- abc=123
+ abc+=123
- klm=10.10
+ klm=10.101
- xyz=6666
+ xyz=666
这样的输出也很受欢迎:
- abc=123
+ abc+=123
def=234
- klm=10.10
+ klm=10.101
- xyz=6666
+ xyz=666
我不想要
stackoverflow=1000
superuser=2000
wtf=911
输出中的行。
有没有办法在 Linux 中通过 diff 参数获得此功能?
最佳答案
一个简单的 Perl 脚本:
use strict;
use warnings;
my ($fname1, $fname2) = ($ARGV[0], $ARGV[1]);
my %conf;
open (my $input1, "<", "$fname1") or die("open $fname1: $!");
while (<$input1>) {
chomp;
my @v = split(/\+?=/);
$conf{$v[0]}=$_;
}
close $input1;
open (my $input2, "<", "$fname2") or die("open $fname2: $!");
while (<$input2>) {
chomp;
my @v = split(/\+?=/);
if (defined ($conf{$v[0]}) && $_ ne $conf{$v[0]}) {
print "- $conf{$v[0]}\n";
print "+ $_\n";
}
}
close $input2;
输出
- abc=123
+ abc+=123
- klm=10.10
+ klm=10.101
- xyz=6666
+ xyz=666
关于linux - 仅显示带有 diff 的已更改行,而不显示新添加的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14134886/