linux - 仅显示带有 diff 的已更改行,而不显示新添加的行

标签 linux diff

我很好奇是否有一种方法可以仅通过 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/

相关文章:

linux - 使用 CTRL-C 终止由 bash 脚本启动的进程

java代码和unix守护进程

android - 如何修复此错误 : spawn EACCES

linux - 使用 Diff 检查文件夹结构中更改的文件,忽略以模式开头的行

Bash:判断一个文件是否包含在另一个文件中

git - 如何在 vim 中并排查看多个 git diff

regex - sed命令显示

node.js - 尝试在Docker容器中安装puppeteer时出错

ios - 查找两个对象之间更改的属性

ruby - 是否有 ruby​​ gem 可以区分 HTML 文档?