使用egrep正则表达式捕获部分行

发布于 2024-12-11 09:28:23 字数 559 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

却一份温柔 2024-12-18 09:28:23

假设您有 GNU grep,请使用 Perl 后视:

git commit -m "$(grep -Po '(?<=Subject: \[PATCH \d/\d\].).*') $patch"

Assuming you have GNU grep, use a Perl look-behind:

git commit -m "$(grep -Po '(?<=Subject: \[PATCH \d/\d\].).*') $patch"
扎心 2024-12-18 09:28:23

在这种情况下,不要使用 -o 来进行egrep(因为您正在匹配一堆您不想打印的内容)。相反,只需匹配整行并将其通过管道传输到“cut”(或 sed,或其他将从行中删除前缀的东西)。

此外,您将 git commit 的输出通过管道传输到egrep,而不提供egrep 作为 git commit 的命令行选项...我想你想要类似的东西:

git commit -m "$(egrep '<your regexp here>' $f | cut -d] -f2-)"

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:

git commit -m "$(egrep '<your regexp here>' $f | cut -d] -f2-)"
度的依靠╰つ 2024-12-18 09:28:23

我会为此使用 sed

git commit -m | sed -r -n 's#^Subject: \[PATCH [0-9]/[0-9]\] ##p;'

I'd use sed for this

git commit -m | sed -r -n 's#^Subject: \[PATCH [0-9]/[0-9]\] ##p;'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文