获取 bash 中两个特殊字符之间的数字
Get the number between two special characters in bash
我有一个实际上是 buildkite 提交消息的字符串,我正在尝试检索两个特殊字符之间的数字(拉取请求编号)。
例如字符串可能类似于
"some commit message (#36)
* test
* unsquashed
* etc
* bla"
使用bash,现在我想从上面的字符串中得到数字36。不幸的是,我将无法将 sed
或 grep
与 perl-regexp
一起使用。
非常感谢您的帮助。
使用参数扩展运算符。
commit_msg="some commit message (#36)
* test
* unsquashed
* etc
* bla"
id=${commit_msg#*(#} # remove everything up to (#
id=${id%%)*} # remove everything starting from )
echo "$id"
我有一个实际上是 buildkite 提交消息的字符串,我正在尝试检索两个特殊字符之间的数字(拉取请求编号)。
例如字符串可能类似于
"some commit message (#36)
* test
* unsquashed
* etc
* bla"
使用bash,现在我想从上面的字符串中得到数字36。不幸的是,我将无法将 sed
或 grep
与 perl-regexp
一起使用。
非常感谢您的帮助。
使用参数扩展运算符。
commit_msg="some commit message (#36)
* test
* unsquashed
* etc
* bla"
id=${commit_msg#*(#} # remove everything up to (#
id=${id%%)*} # remove everything starting from )
echo "$id"