1

I tried fetching only the commit ID from the commit message in the shell script, I need only "d1099308a1af0f91e93bf22cf6e9b5d294cf121d"

commit_message = "commit d1099308a1af0f91e93bf22cf6e9b5d294cf121d Author: Martin Date: Wed Apr 17 16:05:35 2019"

I tried using the following sed command, but it is not working commit_ID=$( sed -e 's/commit .(*) Author/' $commit_message )

Hichem BOUSSETTA
  • 1,733
  • 1
  • 21
  • 25

3 Answers3

1

If you do not have other ID's, this regex will work:

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

If there are other ID's, then adding a look behind will help filtering:

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

However, the "s" command of sed does not fetch, it "substitutes". For fetching, you may want to use "grep" or others.

virolino
  • 1,880
  • 4
  • 20
1

You mean something like that?

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

output

d1099308a1af0f91e93bf22cf6e9b5d294cf121d
UtLox
  • 3,246
  • 2
  • 8
  • 12
0

Give a try to:

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

This will return only the git commit ID SHA-1 hashes (40 digits long).

nbari
  • 23,059
  • 8
  • 65
  • 110