shell Bash脚本字符串操作

k2fxgqgv  于 2023-03-03  发布在  Shell
关注(0)|答案(4)|浏览(148)

我有一个文件名数组,其中包含一些tar文件,例如数组中有两个文件名,acceptance-tests-0.0.134.targrafana-9.3.2-debian-11-r11.tar,我们如何编写一个bash命令,从第一个文件名获取0.0.134,从第二个文件名获取9.3.2-debian-11-r11
我尝试了{imageName##*-},它为我提供了第一个0.0.134的正确答案,但第二个只有r11
这是整个数组的列表,如果有人需要,

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
nc1teljy

nc1teljy1#

大概是这样的

#!/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

将产生:

0.0.134
9.3.2-debian-11-r11
llew8vvj

llew8vvj2#

使用Bash的内置正则表达式支持([[ string =~ pattern ]])-

传真:

arr=( busybox-1.36.tar
      csi-attacher-v3.4.0.tar
      python3-3.11.2-debian-11-r0.tar
      python3-v3.11.2-debian-11-r0.tar
    )

pat='^.*-(v?[.0-9]+(-debian-[-r0-9]+)?)[.]tar$'
for tar in ${arr[@]}; do
    if [[ $tar =~ $pat ]]; then
        echo "$tar  =>  ${BASH_REMATCH[1]}"
    else
        echo "$tar  =>  ERROR"
    fi
done

结果:

$ bash foo.sh
busybox-1.36.tar  =>  1.36
csi-attacher-v3.4.0.tar  =>  v3.4.0
python3-3.11.2-debian-11-r0.tar  =>  3.11.2-debian-11-r0
python3-v3.11.2-debian-11-r0.tar  =>  v3.11.2-debian-11-r0
8ehkhllq

8ehkhllq3#

$ 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
toiithl6

toiithl64#

试试这个Shellcheck-clean pure Bash代码:

#! /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
  • shopt -s extglob支持“扩展的全局绑定”(包括像@(v[0-9]|[0-9])这样的模式),参见glob - Greg's Wiki中的extglob部分。
  • 请参见移除字符串的一部分(BashFAQ/100(如何在bash中进行字符串操作?)),了解${var%pat}${var%%pat}${var#pat}的解释。

相关问题