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

标签 regex bash

我正在尝试通过 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 的提交描述。

最佳答案

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

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

关于regex - 使用egrep正则表达式捕获部分行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7853592/

相关文章:

php - 如何进行非阻塞 php exec 调用?

linux - 重命名文件夹中的所有文件

javascript - MongoDB Like Query(首字母或单词查找查询)

c++ - 自动检测相同的连续 std::string::find() 调用

regex - Sed、awk 或 perl 捕获 2 种模式之间的线条

json - golang 格式数据一次性转换为 JSON 格式

mysql - 使用linux脚本将应用程序输出写入mysql数据库

bash - nohup 和 enter 将停止进程 : why?

python - bash 或 python 命令循环通过终端并在它们上执行命令?

c# - 正则表达式 C#