使用egrep正则表达式捕获部分行
我正在尝试通过 bash 脚本提交 git 补丁。这不是一个 git 问题!这就是我想要做的,我在目录中有一个文件列表。我想一一读取这些文件,从中提取特定行,然后提交。 这是我到目前为止所得到的;
patches=/{location}/*.patch for patch in $patches do echo "Processing $patch file..." git apply $patch git add --all git commit -m | egrep -o "(^Subject: \[PATCH [0-9]\/[0-9]\].)(.*)$" $f echo "Committed $patch file..." done
无法让 egrep 正则表达式传递正确的提交消息。 这是补丁文件中的示例行;
..... Subject: [PATCH 1/3] XSR-2756 Including ldap credentials in property file. ......
我只想捕获“XSR-2756 在属性文件中包含 ldap 凭据。”并用作 git 的提交描述。
I'm trying to commit git patches via a bash script. This is not a git question! Here is what I want to do, I have a list of files in a directory. I want read those files one by one extract a particular line out of it and then commit.
Here is what I got so far;
patches=/{location}/*.patch for patch in $patches do echo "Processing $patch file..." git apply $patch git add --all git commit -m | egrep -o "(^Subject: \[PATCH [0-9]\/[0-9]\].)(.*)$" $f echo "Committed $patch file..." done
Couldn't get the egrep regex working to pass on the proper commit message.
Here is an example line from a patch file;
..... Subject: [PATCH 1/3] XSR-2756 Including ldap credentials in property file. ......
I just want to capture "XSR-2756 Including ldap credentials in property file." and use as a commit description to git.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您有 GNU grep,请使用 Perl 后视:
Assuming you have GNU grep, use a Perl look-behind:
在这种情况下,不要使用
-o
来进行egrep(因为您正在匹配一堆您不想打印的内容)。相反,只需匹配整行并将其通过管道传输到“cut”(或 sed,或其他将从行中删除前缀的东西)。此外,您将 git commit 的输出通过管道传输到egrep,而不提供egrep 作为 git commit 的命令行选项...我想你想要类似的东西:
Don't use the
-o
to egrep in this case (since you're matching a bunch of stuff you don't want printed). Instead, just match the whole line and pipe it to 'cut' (or sed, or something else that will trim a prefix from a line.)Also, you're piping the output of git commit into egrep, not providing the output of egrep as a command line option to git commit... I think you want something like:
我会为此使用 sed
I'd use sed for this