问题描述
我对 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