When archiving certain data we encode the archive's sha1 HASH within the file name so as to determine the integrity of the archive.
I am trying to find a way to automate the integrity check by extracting the HASH out of the file name:
echo myid123_2019-08-31_b7769c0e22c7f75b2935afad499852630ca83145.tar.xz | sed -n 's/^.*\([[:xdigit:]]{40}\).*$/\1/p'
echo myid123_2019-08-31_b7769c0e22c7f75b2935afad499852630ca83145.tar.xz | sed -n 's/^.([0-9a-fA-F]{40}).$/\1/p'
Both tests above return no results. Am I missing something?
I would prefer to test for the HASH explicitly, rather then by elimination or position as the filename format can vary. In any case the hash would be delimited by non-hash characters.
Follow-up:
Thanks for the help.
This is the final product I was looking to create:
function checkhash () {
for f in "$@"
do
test -f $f || continue
export HASH=$(echo ${f}| grep -o '[0-9a-fA-F]\{32,128\}' )
case $(echo -n ${HASH} | wc -c) in
32)
echo "${HASH} *${f}" | md5sum -c -
;;
40)
echo "${HASH} *${f}" | sha1sum -c -
;;
56)
echo "${HASH} *${f}" | sha224sum -c -
;;
64)
echo "${HASH} *${f}" | sha256sum -c -
;;
96)
echo "${HASH} *${f}" | sha384sum -c -
;;
128)
echo "${HASH} *${f}" | sha512sum -c -
;;
*)
echo "No Identified HASH found in filename: ${f}"
;;
esac
done
}
{40}
isn't a quantifier in a sed basic regular expression - you'd needsed -n 's/^.*\([[:xdigit:]]\{40\}\).*$/\1/p'
orsed -En 's/^.*([[:xdigit:]]{40}).*$/\1/p'
. See Why does my regular expression work in X but not in Y? – steeldriver Sep 01 '22 at 00:24