So I have a array of file names, that contain some tar files,
So for example there are 2 file names in the array, acceptance-tests-0.0.134.tar
and grafana-9.3.2-debian-11-r11.tar
.
How to we write a bash command that gets us, 0.0.134
from the first but 9.3.2-debian-11-r11
from the second ?
I tried {imageName##*-}
that gives me the correct answer for the first 0.0.134
but just r11
for the second
Here is the list of the entire array, if someone needs it,
acceptance-tests-0.0.134.tar
alertmanager-0.25.0-debian-11-r4.tar
blackbox-exporter-0.23.0-debian-11-r10.tar
busybox-1.36.tar
cephcsi-v3.5.1.tar
csi-attacher-v3.4.0.tar
csi-node-driver-registrar-v2.4.0.tar
csi-provisioner-v3.1.0.tar
csi-resizer-v1.3.0.tar
csi-snapshotter-v4.2.0.tar
dashboard-test-0.0.134.tar
fluent-bit-2.0.8.tar
grafana-9.3.2-debian-11-r11.tar
graylog-5.0.2.tar
grm-test-0.0.134.tar
kube-state-metrics-2.7.0-debian-11-r9.tar
lrm-sim-test-0.0.134.tar
mongodb-6.0.4-debian-11-r0.tar
node-exporter-1.5.0-debian-11-r9.tar
opensearch-2.3.0.tar
pcs-sim-test-0.0.134.tar
postgresql-repmgr-15.1.0-debian-11-r22.tar
prometheus-2.41.0-debian-11-r5.tar
prometheus-operator-0.62.0-debian-11-r0.tar
sftp-5.1.5.tar
system-tests-0.0.134.tar
3
Answers
Something like this:
Will produce:
Try this Shellcheck-clean pure Bash code:
shopt -s extglob
enables "extended globbing" (including patterns like@(v[0-9]|[0-9])
). See the extglob section in glob – Greg’s Wiki.${var%pat}
,${var%%pat}
, and${var#pat}
.