skip to Main Content

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


  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 "$fn"
    done
    

    Will produce:

    0.0.134
    9.3.2-debian-11-r11
    
    Login or Signup to reply.
  2. $ awk '{ print gensub(/.*-(v?[0-9]+..*).tar/,"\1","g") }' <(printf "%sn" "${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 "%sn" "${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
    
    Login or Signup to reply.
  3. 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: %qn' "$tf" >&2
        else
            ver=${name_ver#"$name"-}
            printf '%s => %sn' "$tf" "$ver"
        fi
    done
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search