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,791
  • 1
  • 21
  • 27

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
  • 2,073
  • 5
  • 21
1

You mean something like that?

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

output

d1099308a1af0f91e93bf22cf6e9b5d294cf121d
UtLox
  • 3,744
  • 2
  • 10
  • 13
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
  • 25,603
  • 10
  • 76
  • 131