0

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
2
  • Are the filenames in an array or in a file, with one name per line? Commented Feb 28, 2023 at 14:56
  • 1
    How would you explain, with your own words and in plain English, what you want to achieve? What is the rule that tells which - is the last to remove? Once you'll have answered this, and checked that it works with all examples you have, coding will be much easier. Commented Feb 28, 2023 at 16:25

3 Answers 3

1

Something like this:

#!/usr/bin/env bash

declare -a files=(
  acceptance-tests-0.0.134.tar
  grafana-9.3.2-debian-11-r11.tar
)

shopt -s extglob # enable extended pattern matching

for f in ${files[@]}; do
  f=${f##+([[:alpha:]-])}
  f=${f%.*}
  printf "$f\n"
done

Will produce:

0.0.134
9.3.2-debian-11-r11
Sign up to request clarification or add additional context in comments.

2 Comments

OP may have not realized but it's very common for a pkg name to include digit chars (like python3).
@pynexj absolutely, you're right. I will leave my answer as is, since your own already offers a better alternative.
0
$ awk '{ print gensub(/.*-(v?[0-9]+\..*).tar/,"\\1","g") }' <(printf "%s\n" "${myarray[@]}")

0.0.134
0.25.0-debian-11-r4
0.23.0-debian-11-r10
...

$ awk '{ printf "%s \n", $0; print gensub(/(.*)-(v?[0-9]+\..*).tar/,"\\1 ==> \\2","g") }' \
     <(printf "%s\n" "${myarray[@]}")

acceptance-tests-0.0.134.tar 
acceptance-tests ==> 0.0.134
alertmanager-0.25.0-debian-11-r4.tar
alertmanager ==> 0.25.0-debian-11-r4
blackbox-exporter-0.23.0-debian-11-r10.tar
blackbox-exporter ==> 0.23.0-debian-11-r10

Comments

0

Try this Shellcheck-clean pure Bash code:

#! /bin/bash -p

tarfiles=(  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                        )

shopt -s extglob
for tf in "${tarfiles[@]}"; do
    name_ver=${tf%.tar}
    name=${name_ver%%-@(v[0-9]|[0-9])*}
    if [[ $name == "$name_ver" ]]; then
        printf 'ERROR: Cannot extract version from: %q\n' "$tf" >&2
    else
        ver=${name_ver#"$name"-}
        printf '%s => %s\n' "$tf" "$ver"
    fi
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.