Usando grupos de captura e $BASH_REMATCH
para extrair bits de sequências:
for name in *.log.*; do
if [[ "$name" =~ \.([0-9]{4})_([0-9]{2})_([0-9]{2}) ]]; then
printf '%d-%d-%d from "%s"\n' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[3]}" \
"$name"
fi
done
Saída:
2017-01-26 from "abcd.log.2017_01_26_23_30.0"
2017-01-26 from "abcd.log.2017_01_26_23_35.0"
2017-02-20 from "abcd.log.2017_02_20_23_10.0"
Se você precisar da string de data em uma variável:
for name in *.log.*; do
if [[ "$name" =~ \.([0-9]{4})_([0-9]{2})_([0-9]{2}) ]]; then
datestring="$( printf '%d-%d-%d' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}" )"
printf '%s from "%s"\n' "$datestring" "$name"
fi
done
Em ksh93
, substitua BASH_REMATCH
por .sh.match
.