regex - 解析 git 历史记录并对其进行模式匹配,一旦找到匹配项,就在变量中捕获附近的值
问题描述
我有很长的 git 历史,我想找到“build(mariadbstuff): Copybara import”的第一个实例,然后在变量中捕获其关联的 GitOrigin-RevId“d7b349b6b6db24047ae32a1618dd5f5544864225”。
我有一种非常hacky,丑陋的方式,我希望有一种更清洁的方式。这是完成这项工作的正确但笨拙且丑陋的 bash cmd(尽管它不会删除 GitOriginRevId 部分):
sed -e '/build(mariadbstuff)/,/GitOriginRevId/!d' <(git log) > outputfile.txt && sed -n 11p outputfile.txt
... // more contents of git history
build(mariadbstuff): Copybara import
Squashed commit of the following:
commit d8829cc7df7a6813f93d224916b10e7c148950be
Author: Copybara <blah-tools@blah.com>
Date: Fri Jul 10 10:26:41 2020 -0400
Project import generated by Copybara.
GitOrigin-RevId: d7b349b6b6db24047ae32a1618dd5f5544864225
... // more contents of git history
解决方案
您能否尝试在 GNU 中使用显示的示例进行跟踪、编写和测试awk
。此解决方案尽快退出 Input_file,它会获得 OP 所需的 id,并且不会读取整个 Input_file,这将比读取整个 Input_file 解决方案更快。
awk '
/build\(mariadbstuff\): Copybara/{
found=1
next
}
found && /GitOrigin-RevId:/{
print $NF
exit
}
' Input_file
说明:为上述添加详细说明。
awk ' ##Starting awk program from here.
/build\(mariadbstuff\): Copybara/{ ##Checking condition if a line contains string build\(mariadbstuff\): Copybara escaping ( and ) in it to treat it as literal character here.
found=1 ##Setting found variable to 1 here.
next ##next will skip all further statements from here.
}
found && /GitOrigin-RevId:/{ ##Checking condition if found is SET and line has string GitOrigin-RevId: then do following.
print $NF ##Printing last column of current line here.
exit ##exiting from program from here to save sometime :)
}
' Input_file ##Mentioning Input_file name here.