微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Grepdiff 没有正确匹配行的开头或结尾

如何解决Grepdiff 没有正确匹配行的开头或结尾

我对 grepdiff 中的某些行为感到困惑。我正在尝试编写一个脚本来解析差异的各个大块并寻找模式。特别是,我正在寻找添加删除仅对数值进行微小更改的行的差异,如下所示:

@@ -2160 +2160 @@
-        "posZ": 13.912,+        "posZ": 13.911,

我的目标是让脚本恢复这些更改。为此,我需要先检测它们。但是,我无法让 grepdiff 正确识别行。

这个正则表达式匹配:

git diff -U0 HEAD^ | grepdiff -E '^[^0-9-]*[0-9-]+.[0-9]+,' --output-matching=hunk 

这不会:

git diff -U0 HEAD^ | grepdiff -E '^[^0-9-]*[0-9-]+.[0-9]+,$' --output-matching=hunk 

同样,这也不是:

git diff -U0 HEAD^ | grepdiff -E '^\+[^0-9-]*[0-9-]+.[0-9]+,' --output-matching=hunk

如果没有正确匹配行的开头或结尾,我不知道如何识别这些大块头。

解决方法

使用

grepdiff -E '^[^0-9-]*[0-9-]+[.][0-9]+,[[:space:]]*$'

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [^0-9-]*                 any character except: '0' to '9','-' (0
                           or more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [0-9-]+                  any character of: '0' to '9','-' (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [.]                      any character of: '.'
--------------------------------------------------------------------------------
  [0-9]+                   any character of: '0' to '9' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------,','
--------------------------------------------------------------------------------
  [[:space:]]*             any character of: whitespace characters
                           (like \s) (0 or more times (matching the
                           most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n,and the end of the
                           string

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。