如何从shell脚本中的完整提交消息中单独提取提交ID

问题描述 投票:1回答:3

我尝试从shell脚本中的提交消息中仅提取提交ID,我只需要“d1099308a1af0f91e93bf22cf6e9b5d294cf121d”

commit_message =“commit d1099308a1af0f91e93bf22cf6e9b5d294cf121d作者:Martin日期:Wed Apr 17 16:05:35 2019”

我尝试使用以下sed命令,但它不工作commit_ID = $(sed -e's / commit。(*)Author /'$ commit_message)

linux shell
3个回答
1
投票

你的意思是那样的?

sed 's/^commit \([^ ]*\).*/\1/' <<< $commit_message

产量

d1099308a1af0f91e93bf22cf6e9b5d294cf121d

0
投票

如果您没有其他ID,this regex将起作用:

[0-9a-fA-F]{20,}

如果还有其他ID,那么添加a look behind将有助于过滤:

(?<="commit\s)[0-9a-fA-F]{20,}

但是,sed的“s”命令不能获取,它“替代”。对于提取,您可能想要使用“grep”或其他。


0
投票

尝试:

egrep -o "[a-f0-9]{40}" log.txt

这将仅返回git提交ID SHA-1 hashes(长度为40位)。

© www.soinside.com 2019 - 2024. All rights reserved.