
Der Benutzer stellt eine Liste mit Bereichen bereit. Ich muss sie in eine Liste mit diskreten Werten umwandeln.
Zum Beispiel
Eingang
host[1-5],host7,host[13-15]
Ausgabe
host1,host2,host3,host4,host5,host7,host13,host14,host15.
Wie kann ich dieses Ergebnis erreichen?
Antwort1
Versuche dies:
hosts=($(echo "host[1-5],host7,host[13-15]" | sed 's/,/ /g'))
string=""
for h in ${hosts[@]}; do
if [[ ! ${h//[^0-9]/} =~ ^[0-9]$ ]]; then
for i in $(seq ${h//[^0-9]/ }); do
string+="${h%[*}$i,"
done
else
string+="$h,"
fi
done
echo "${string%,}"
Ausgabe:
host1,host2,host3,host4,host5,host7,host13,host14,host15,
Antwort2
Versuchen Sie bash
es mit „Parametererweiterung / Mustersubstitution“
$ hostlist=host[1-5],host7,host[13-15]
$ H1=${hostlist//[/{}
$ H2=${H1//]/\}}
$ H3=${H2//,/ }
$ H4=$(eval echo ${H3//-/..})
$ H5=${H4// /,}
$ echo "$H5"
host1,host2,host3,host4,host5,host7,host13,host14,host15
Antwort3
Wenn Sie Ihre Eingabe ändern in
hostlist=host{{1..5},7,{13..15}}
Dann
eval echo $hostlist | tr ' ' ,
host1,host2,host3,host4,host5,host7,host13,host14,host15