我尝试从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)
你的意思是那样的?
sed 's/^commit \([^ ]*\).*/\1/' <<< $commit_message
产量
d1099308a1af0f91e93bf22cf6e9b5d294cf121d
如果您没有其他ID,this regex将起作用:
[0-9a-fA-F]{20,}
如果还有其他ID,那么添加a look behind将有助于过滤:
(?<="commit\s)[0-9a-fA-F]{20,}
但是,sed的“s”命令不能获取,它“替代”。对于提取,您可能想要使用“grep”或其他。